Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Convert a number into words, version 2 number, words, hundred, thousand, million Algorithms

By Bill Hileman.

See the code for details. One key step formats the value to have 8 digits before the decimal point. That limits this function to values under 100 million.

```Public Function VerbalDollar(curAmount As Currency) As _
String

Const strHundred = "Hundred"
Const strThousand = "Thousand"
Const strMillion = "Million"
Const strDollars = "Dollars"
Const strAnd = "And"
Const strHundredths = "/100"

Dim varDigit As Variant

Dim strVerbal As String
Dim strNumber As String

Dim intCnt1 As Integer
Dim intCnt2 As Integer
Dim intMillions As Integer
Dim intThousands As Integer
Dim intHundreds As Integer
Dim intTens As Integer

On Error GoTo VerbalDollar_Error

varDigit = Array("Zero", "One", "Two", "Three", "Four", _
"Five", "Six", "Seven", _
"Eight", "Nine", "Ten", "Eleven", _
"Twelve", "Thirteen", "Fourteen", _
"Fifteen", "Sixteen", "Seventeen", _
"Eighteen", "Nineteen", "Twenty")

ReDim Preserve varDigit(99)

varDigit(30) = "Thirty"
varDigit(40) = "Forty"
varDigit(50) = "Fifty"
varDigit(60) = "Sixty"
varDigit(70) = "Seventy"
varDigit(80) = "Eighty"
varDigit(90) = "Ninety"

For intCnt1 = 20 To 90 Step 10
For intCnt2 = 1 To 9
varDigit(intCnt1 + intCnt2) = varDigit(intCnt1) & "-" _
& StrConv(varDigit(intCnt2), vbLowerCase)
Next intCnt2
Next intCnt1

strVerbal = ""

strNumber = Format\$(Format\$(curAmount, "#######0.00"), _
"@@@@@@@@@@@")

intMillions = Val(Left\$(strNumber, 2))

If intMillions <> 0 Then
strVerbal = varDigit(intMillions) & " " & strMillion & _
" "
End If

intThousands = Val(Mid\$(strNumber, 3, 3))

If intThousands <> 0 Then
intHundreds = Val(Mid\$(strNumber, 3, 1))
If intHundreds <> 0 Then
strVerbal = strVerbal & varDigit(intHundreds) & " " & _
strHundred & " "
End If
intTens = Val(Mid\$(strNumber, 4, 2))
If intTens <> 0 Then
strVerbal = strVerbal & varDigit(intTens) & " "
End If
strVerbal = strVerbal & strThousand & " "
End If

intHundreds = Val(Mid\$(strNumber, 6, 1))

If intHundreds <> 0 Then
strVerbal = strVerbal & varDigit(intHundreds) & " " & _
strHundred & " "
End If

intTens = Val(Mid\$(strNumber, 7, 2))

If intTens <> 0 Then
strVerbal = strVerbal & varDigit(intTens) & " "
End If

If strVerbal = "" Then
strVerbal = varDigit(0) & " "
End If

strVerbal = strVerbal & strAnd & " " & Right\$(strNumber, _
2) & strHundredths & " " & strDollars

VerbalDollar = strVerbal

On Error GoTo 0
Exit Function

VerbalDollar_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure VerbalDollar of Module mDASIGlobal"

End Function```