Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleConvert really huge numbers into words in Visual Basic .NET
DescriptionThis example shows how to convert really huge numbers into words in Visual Basic .NET.
Keywordsconvert number, number, words, hundred, thousand, million, billion, trillion, quadrillion, milliard
CategoriesAlgorithms, VB.NET
 
Function GroupToWords returns a string representing a number between 1 and 999. If the number is 0, it returns an empty string.

Next if the number is greater than 99, the function gets the hundreds digit and uses the one_to_nineteen array to look up its string representation. It adds that representation plus the word "hundred" to the result. If the number has no other digits, the function returns what it has so far.

Next if the remaining number is less than 20, the function uses the one_to_nineteen array to look up the number's representation.

If the number is greater than 19, the function gets the tens digit and uses the multiples_of_ten array to get its representation. It then uses the one_to_nineteen array to add on the representation of the ones digit.

 
' Convert a number between 0 and 999 into words.
Private Function GroupToWords(ByVal num As Integer) As _
    String
    Static one_to_nineteen() As String = {"zero", "one", _
        "two", "three", "four", "five", "six", "seven", _
        "eight", "nine", "ten", "eleven", "twelve", _
        "thirteen", "fourteen", "fifteen", "sixteen", _
        "seventeen", "eightteen", "nineteen"}
    Static multiples_of_ten() As String = {"twenty", _
        "thirty", "forty", "fifty", "sixty", "seventy", _
        "eighty", "ninety"}

    ' If the number is 0, return an empty string.
    If num = 0 Then Return ""

    ' Handle the hundreds digit.
    Dim digit As Integer
    Dim result As String = ""
    If num > 99 Then
        digit = num \ 100
        num = num Mod 100
        result = one_to_nineteen(digit) & " hundred"
    End If

    ' If num = 0, we have hundreds only.
    If num = 0 Then Return result.Trim()

    ' See if the rest is less than 20.
    If num < 20 Then
        ' Look up the correct name.
        result &= " " & one_to_nineteen(num)
    Else
        ' Handle the tens digit.
        digit = num \ 10
        num = num Mod 10
        result &= " " & multiples_of_ten(digit - 2)

        ' Handle the final digit.
        If num > 0 Then
            result &= " " & one_to_nineteen(num)
        End If
    End If

    Return result.Trim()
End Function
 
Function NumberToString takes a number as a string input and returns a string representation of the number. It takes the input as a string to allow really huge numbers. The Decimal and Double data types cannot store truly enormous numbers without precision errors.

First the function checks its use_us_group_names parameter and initializes its groups array to a list of group names that is either appropriate for US/scientific use or for non-US use.

Next the function cleans up its input string a bit. It removes "$", "," and leading zeros. If the string contains a decimal point, it removes anything after it.

The code then determines how many groups of three digits the number needs. It pads the string on the left so its length is a multiple of 3.

Now the code steps through the number's three-digit groups. For each group, it extracts the digits, converts them into a number, uses function GroupToWords to convert the group into a string, and adds the group's name to the result.

Finally the function removes the trailing ", " if present and returns the result.

 
' Return a word representation of the whole number value.
Private Function NumberToString(ByVal num_str As String, _
    Optional ByVal use_us_group_names As Boolean = True) As _
    String
    ' Get the appropiate group names.
    Dim groups() As String
    If use_us_group_names Then
        groups = New String() {"", "thousand", "million", _
            "billion", "trillion", "quadrillion", _
            "quintillion", "sextillion", "septillion", _
            "octillion", "nonillion", "decillion", _
            "undecillion", "duodecillion", "tredecillion", _
            "quattuordecillion", "quindecillion", _
            "sexdecillion", "septendecillion", _
            "octodecillion", "novemdecillion", _
            "vigintillion"}
    Else
        groups = New String() {"", "thousand", "million", _
            "milliard", "billion", "1000 billion", _
            "trillion", "1000 trillion", "quadrillion", _
            "1000 quadrillion", "quintillion", "1000 " & _
            "quintillion", "sextillion", "1000 sextillion", _
            "septillion", "1000 septillion", "octillion", _
            "1000 octillion", "nonillion", "1000 " & _
            "nonillion", "decillion", "1000 decillion"}
    End If

    ' Clean the string a bit.
    ' Remove "$", ",", leading zeros, and
    ' anything after a decimal point.
    Const CURRENCY As String = "$"
    Const SEPARATOR As String = ","
    Const DECIMAL_POINT As String = "."
    num_str = num_str.Replace(CURRENCY, _
        "").Replace(SEPARATOR, "")
    num_str = num_str.TrimStart(New Char() {"0"c})
    Dim pos As Integer = num_str.IndexOf(DECIMAL_POINT)
    If pos = 0 Then
        Return "zero"
    ElseIf pos > 0 Then
        num_str = num_str.Substring(0, pos - 1)
    End If

    ' See how many groups there will be.
    Dim num_groups As Integer = (num_str.Length + 2) \ 3

    ' Pad so length is a multiple of 3.
    num_str = num_str.PadLeft(num_groups * 3, " "c)

    ' Process the groups, largest first.
    Dim result As String = ""
    Dim group_num As Integer
    For group_num = num_groups - 1 To 0 Step -1
        ' Get the next three digits.
        Dim group_str As String = num_str.Substring(0, 3)
        num_str = num_str.Substring(3)
        Dim group_value As Integer = CInt(group_str)

        ' Convert the group into words.
        If group_value > 0 Then
            If group_num >= groups.Length Then
                result &= GroupToWords(group_value) & _
                    " ?, "
            Else
                result &= GroupToWords(group_value) & _
                    " " & groups(group_num) & ", "
            End If
        End If
    Next group_num

    ' Remove the trailing ", ".
    If result.EndsWith(", ") Then
        result = result.Substring(0, result.Length - 2)
    End If

    Return result.Trim()
End Function
 
To convert a number into a string, the program simply calls function NumberToString and displays the result.
 
txtResult.Text = NumberToString(txtNumber.Text, _
    chkUseUsGroupNames.Checked)
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated