' Convert a number between 0 and 999 into words.
Public Function NumToWords(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