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 a number into words
Keywordsnumber, words, hundred, thousand, million
CategoriesAlgorithms
 
Examine the number three digits at a time. Translate each three-digit group into words and add the appropriate ending as in "twenty two thousand."

Function Words_1_999 returns a string for a number between 1 and 999. It pulls off the hundreds digit and evaluates it if it is non-zero using function Words_1_19. It then uses function Words_1_99 to evaluate the remainder (tens and ones digits) which form a number between 1 and 19.

Function Words_1_19 uses a Select Case statement to convert numbers between 1 and 19 into words.

Function Words_1_99 determines whether the number is less than 20. If it is, the function uses Words_1_19 to find its string value.

If the number is greater than 19, the function examines the tens digit to get the appropriate string. It then uses Words_1_19 to find the appropriate word for the ones digit.

Function Words_1_all uses these other functions to calculate the value for the entire number. It makes an array of grouping endings: thousand, million, and so forth. (Insert milliard, thousand million, etc. if you wish.) It then decides whether the number is greater than each group's smallest value and, if it is, uses Words_1_999 to calculate an appropriate string.

Function Words_Money breaks the number at the decimal point and converts the two pieces into dollars and cents. (You could change it to pounds, euros, or whatever).

 
' Return words for this value between 1 and 999.
Private Function Words_1_999(ByVal num As Integer) As String
Dim hundreds As Integer
Dim remainder As Integer
Dim result As String

    hundreds = num \ 100
    remainder = num - hundreds * 100

    If hundreds > 0 Then
        result = Words_1_19(hundreds) & " hundred "
    End If

    If remainder > 0 Then
        result = result & Words_1_99(remainder)
    End If

    Words_1_999 = Trim$(result)
End Function

' Return a word for this value between 1 and 19.
Private Function Words_1_19(ByVal num As Integer) As String
    Select Case num
        Case 1
            Words_1_19 = "one"
        Case 2
            Words_1_19 = "two"
        Case 3
            Words_1_19 = "three"
        Case 4
            Words_1_19 = "four"
        Case 5
            Words_1_19 = "five"
        Case 6
            Words_1_19 = "six"
        Case 7
            Words_1_19 = "seven"
        Case 8
            Words_1_19 = "eight"
        Case 9
            Words_1_19 = "nine"
        Case 10
            Words_1_19 = "ten"
        Case 11
            Words_1_19 = "eleven"
        Case 12
            Words_1_19 = "twelve"
        Case 13
            Words_1_19 = "thirteen"
        Case 14
            Words_1_19 = "fourteen"
        Case 15
            Words_1_19 = "fifteen"
        Case 16
            Words_1_19 = "sixteen"
        Case 17
            Words_1_19 = "seventeen"
        Case 18
            Words_1_19 = "eightteen"
        Case 19
            Words_1_19 = "nineteen"
    End Select
End Function

' Return a word for this value between 1 and 99.
Private Function Words_1_99(ByVal num As Integer) As String
Dim result As String
Dim tens As Integer

    tens = num \ 10

    If tens <= 1 Then
        ' 1 <= num <= 19
        result = result & " " & Words_1_19(num)
    Else
        ' 20 <= num
        ' Get the tens digit word.
        Select Case tens
            Case 2
                result = "twenty"
            Case 3
                result = "thirty"
            Case 4
                result = "forty"
            Case 5
                result = "fifty"
            Case 6
                result = "sixty"
            Case 7
                result = "seventy"
            Case 8
                result = "eighty"
            Case 9
                result = "ninety"
        End Select

        ' Add the ones digit number.
        result = result & " " & Words_1_19(num - tens * 10)
    End If

    Words_1_99 = Trim$(result)
End Function

' Return a string of words to represent the
' integer part of this value.
Private Function Words_1_all(ByVal num As Currency) As _
    String
Dim power_value(1 To 5) As Currency
Dim power_name(1 To 5) As String
Dim digits As Integer
Dim result As String
Dim i As Integer

    ' Initialize the power names and values.
    power_name(1) = "trillion": power_value(1) = _
        1000000000000#
    power_name(2) = "billion":  power_value(2) = 1000000000
    power_name(3) = "million":  power_value(3) = 1000000
    power_name(4) = "thousand": power_value(4) = 1000
    power_name(5) = "":         power_value(5) = 1

    For i = 1 To 5
        ' See if we have digits in this range.
        If num >= power_value(i) Then
            ' Get the digits.
            digits = Int(num / power_value(i))

            ' Add the digits to the result.
            If Len(result) > 0 Then result = result & ", "
            result = result & _
                Words_1_999(digits) & _
                " " & power_name(i)

            ' Get the number without these digits.
            num = num - digits * power_value(i)
        End If
    Next i

    Words_1_all = Trim$(result)
End Function

' Return a string of words to represent this
' currency value in dollars and cents.
Private Function Words_Money(ByVal num As Currency) As _
    String
Dim dollars As Currency
Dim cents As Integer
Dim dollars_result As String
Dim cents_result As String

    ' Dollars.
    dollars = Int(num)
    dollars_result = Words_1_all(dollars)
    If Len(dollars_result) = 0 Then dollars_result = "zero"

    If dollars_result = "one" Then
        dollars_result = dollars_result & " dollar"
    Else
        dollars_result = dollars_result & " dollars"
    End If

    ' Cents.
    cents = CInt((num - dollars) * 100#)
    cents_result = Words_1_all(cents)
    If Len(cents_result) = 0 Then cents_result = "zero"

    If cents_result = "one" Then
        cents_result = cents_result & " cent"
    Else
        cents_result = cents_result & " cents"
    End If

    ' Combine the results.
    Words_Money = dollars_result & _
        " and " & cents_result
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated