'arnelgp
Public Function SpellDoler(ByVal num As Double, Optional ByVal properCase As Boolean = False) As String
Dim i As Integer, sFrac As String
Dim sNum As String
sNum = CStr(num)
i = InStrRev(sNum, ".")
If i <> 0 Then
sFrac = Mid$(sNum, i)
'remove the fraction part
sNum = Replace$(sNum, sFrac, "")
sFrac = Right$(sFrac, Len(sFrac) - 1)
If Len(sFrac) > 2 Then
sFrac = Left$(sFrac, 2)
End If
End If
If Len(sFrac) <> 0 Then
sNum = NumberToString(Val(sNum)) & " doler and " & NumberToString(sFrac) & " praisas"
Else
sNum = NumberToString(Val(sNum)) & " doler"
End If
'remove the ","
sNum = Replace$(sNum, ",", "")
If properCase Then
sNum = StrConv(sNum, vbProperCase)
End If
SpellDoler = sNum
End Function
'http://vb-helper.com/howto_number_to_words3.html
' Return a word representation of the whole number value.
Public Function NumberToString(ByVal num_str As String, _
Optional ByVal use_us_group_names As Boolean = True) As _
String
Const CURRENCY_CHAR As String = "$"
Const SEPARATOR As String = ","
Const DECIMAL_POINT As String = "."
Dim groups() As String
Dim pos As Integer
Dim num_groups As Integer
Dim result As String
Dim group_num As Integer
Dim group_str As String
Dim group_value As Integer
' Get the appropiate group names.
If use_us_group_names Then
groups = Split(",thousand,million,billion," & _
"trillion,quadrillion,quintillion," & _
"sextillion,septillion,octillion," & _
"nonillion,decillion,undecillion," & _
"duodecillion,tredecillion," & _
"quattuordecillion,quindecillion," & _
"sexdecillion,septendecillion," & _
"octodecillion,novemdecillion," & _
"vigintillion", ",")
Else
groups = Split(",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.
num_str = Replace$(num_str, CURRENCY_CHAR, "")
num_str = Replace$(num_str, SEPARATOR, "")
Do While Left$(num_str, 1) = "0"
num_str = Mid$(num_str, 2)
Loop
pos = InStr(num_str, DECIMAL_POINT)
If pos = 1 Then
NumberToString = "zero"
Exit Function
ElseIf pos > 1 Then
num_str = Left$(num_str, pos - 1)
End If
' See how many groups there will be.
num_groups = (Len(num_str) + 2) \ 3
' Pad so length is a multiple of 3.
num_str = Space$(num_groups * 3 - Len(num_str)) & _
num_str
' Process the groups, largest first.
result = ""
For group_num = num_groups - 1 To 0 Step -1
' Get the next three digits.
group_str = Mid$(num_str, 1, 3)
num_str = Mid$(num_str, 4)
group_value = CInt(group_str)
' Convert the group into words.
If group_value > 0 Then
If group_num >= UBound(groups) Then
result = result & GroupToWords(group_value) _
& _
" ?, "
Else
result = result & GroupToWords(group_value) _
& _
" " & groups(group_num) & ", "
End If
End If
Next group_num
' Remove the trailing ", ".
If Right$(result, 2) = ", " Then
result = Left$(result, Len(result) - 2)
ElseIf Len(result) = 0 Then
result = "zero"
End If
NumberToString = Trim$(result)
End Function
' Convert a number between 0 and 999 into words.
Private Function GroupToWords(ByVal num As Integer) As _
String
Static done_before As Boolean
Static one_to_nineteen() As String
Static multiples_of_ten() As String
Dim digit As Integer
Dim result As String
If Not done_before Then
done_before = True
one_to_nineteen = Split("zero,one,two,three," & _
"four,five,six,seven,eight,nine,ten," & _
"eleven,twelve,thirteen,fourteen,fifteen," & _
"sixteen,seventeen,eightteen,nineteen", ",")
multiples_of_ten = Split("twenty,thirty," & _
"forty,fifty,sixty,seventy,eighty,ninety", ",")
End If
' If the number is 0, return an empty string.
GroupToWords = 0
If num = 0 Then Exit Function
' Handle the hundreds digit.
result = ""
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
GroupToWords = Trim$(result)
Exit Function
End If
' See if the rest is less than 20.
If num < 20 Then
' Look up the correct name.
result = result & " " & one_to_nineteen(num)
Else
' Handle the tens digit.
digit = num \ 10
num = num Mod 10
result = result & " " & multiples_of_ten(digit - 2)
' Handle the final digit.
If num > 0 Then
result = result & " " & one_to_nineteen(num)
End If
End If
GroupToWords = Trim$(result)
End Function