What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleConvert a number into words in Visual Basic .NET
DescriptionThis example shows how to convert a number into words in Visual Basic .NET.
Keywordsconvert number, number, words, hundred, thousand, million, billion, trillion, quadrillion
CategoriesAlgorithms, VB.NET
Function GroupToWords returns a string representing a number between 1 and 999. If the number is 0, it simply returns "zero."

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 _
    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)
        ' 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 returns a string representation of a longer whole number. If the number is 0, it simply returns "zero."

The function enters a loop to process each group of three digits: ones, thousands, millions, and so forth. Each time through the loop, the function gets the next group's value. It calls function GroupToWords to get a string representation of the group and adds the appropriate group name after it (e.g. thousands).

' Return a word representation of the whole number value.
Private Function NumberToString(ByVal num As Double) As _
    ' Remove any fractional part.
    num = Int(num)

    ' If the number is 0, return zero.
    If num = 0 Then Return "zero"

    Static groups() As String = {"", "thousand", "million", _
        "billion", "trillion", "quadrillion", "?", "??", _
        "???", "????"}
    Dim result As String = ""

    ' Process the groups, smallest first.
    Dim quotient As Double
    Dim remainder As Integer
    Dim group_num As Integer = 0
    Do While num > 0
        ' Get the next group of three digits.
        quotient = Int(num / 1000)
        remainder = CInt(num - quotient * 1000)
        num = quotient

        ' Convert the group into words.
        result = GroupToWords(remainder) & _
            " " & groups(group_num) & ", " & _

        ' Get ready for the next group.
        group_num += 1

    ' 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(CDbl(txtNumber.Text))
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.