See the following article for converting number into words (like 2000 in Two Thausand)
Public Function numtoword(numstr As Variant) As String ' The best data type to feed in is ' Decimal, but it is up to you
Dim tempstr As String Dim newstr As String numstr = CDec(numstr)
If numstr = 0 Then numtoword = "zero " Exit Function End If
If numstr > 10 ^ 24 Then numtoword = "Too big" Exit Function End If
If numstr >= 10 ^ 12 Then newstr = numtoword(Int(numstr / 10 ^ 12)) numstr = ((numstr / 10 ^ 12) - _ Int(numstr / 10 ^ 12)) * 10 ^ 12 If numstr = 0 Then tempstr = tempstr & newstr & "billion " Else tempstr = tempstr & newstr & "billion, " End If End If
If numstr >= 10 ^ 6 Then newstr = numtoword(Int(numstr / 10 ^ 6)) numstr = ((numstr / 10 ^ 6) - _ Int(numstr / 10 ^ 6)) * 10 ^ 6 If numstr = 0 Then tempstr = tempstr & newstr & "million " Else tempstr = tempstr & newstr & "million, " End If End If
If numstr >= 10 ^ 3 Then newstr = numtoword(Int(numstr / 10 ^ 3)) numstr = ((numstr / 10 ^ 3) - _ Int(numstr / 10 ^ 3)) * 10 ^ 3 If numstr = 0 Then tempstr = tempstr & newstr & "thousand " Else tempstr = tempstr & newstr & "thousand, " End If End If
If numstr >= 10 ^ 2 Then newstr = numtoword(Int(numstr / 10 ^ 2)) numstr = ((numstr / 10 ^ 2) - _ Int(numstr / 10 ^ 2)) * 10 ^ 2 If numstr = 0 Then tempstr = tempstr & newstr & "hundred " Else tempstr = tempstr & newstr & "hundred and " End If End If
If numstr >= 20 Then Select Case Int(numstr / 10) Case 2 tempstr = tempstr & "twenty " Case 3 tempstr = tempstr & "thirty " Case 4 tempstr = tempstr & "forty " Case 5 tempstr = tempstr & "fifty " Case 6 tempstr = tempstr & "sixty " Case 7 tempstr = tempstr & "seventy " Case 8 tempstr = tempstr & "eighty " Case 9 tempstr = tempstr & "ninety " End Select numstr = ((numstr / 10) - _ Int(numstr / 10)) * 10 End If
If numstr > 0 Then Select Case numstr Case 1 tempstr = tempstr & "one " Case 2 tempstr = tempstr & "two " Case 3 tempstr = tempstr & "three " Case 4 tempstr = tempstr & "four " Case 5 tempstr = tempstr & "five " Case 6 tempstr = tempstr & "six " Case 7 tempstr = tempstr & "seven " Case 8 tempstr = tempstr & "eight " Case 9 tempstr = tempstr & "nine " Case 10 tempstr = tempstr & "ten " Case 11 tempstr = tempstr & "eleven " Case 12 tempstr = tempstr & "twelve " Case 13 tempstr = tempstr & "thirteen " Case 14 tempstr = tempstr & "fourteen " Case 15 tempstr = tempstr & "fifteen " Case 16 tempstr = tempstr & "sixteen " Case 17 tempstr = tempstr & "seventeen " Case 18 tempstr = tempstr & "eighteen " Case 19 tempstr = tempstr & "nineteen " End Select numstr = ((numstr / 10) - Int(numstr / 10)) * 10 End If numtoword = tempstr End Function
Thanks to an anonymous person from Cheats World for this alternative, shorter function:
Dim Number2s(2 To 9) As String Dim Number1s(1 To 19) As String Dim Steps(1 To 4) As String
Public Function NumToWord(Numstring As Variant) As String If Steps(1) = "" Then initstrings Dim Tempstring As String Dim Newstring As String Dim What As Double
If Numstring = 0 Then NumToWord = "zero" Exit Function End If
If Numstring > (10 ^ 24) Then _ NumToWord = "Number is too big" Exit Function End If
Counter = 0 Do Counter = Counter + 1 If Counter > 1 Then What = 10 ^ (12 \ ((Counter - 1) * 2)) Else What = 10 ^ 12 End If
If Numstring >= What Then Newstring = NumToWord(Fix(Numstring / What)) Numstring = ((Numstring / What) - Fix(Numstring / What)) * What If Numstring = 0 Then Tempstring = Tempstring & Newstring & Steps(Counter) & " " Else If Counter = 4 Then Tempstring = Tempstring & Newstring & _ Steps(Counter) & " and ": GoToSkipit Tempstring = Tempstring & Newstring & _ Steps(Counter) & ", " Skipit: End If End If
Loop Until Counter = 4
If Numstring >= 20 Then _ Tempstring = Tempstring & _ Number2s(Fix(Numstring / 10)) & " "
If Numstring >= 10 And Numstring < 20 Then _ Tempstring = Tempstring & Number1s(CInt(Numstring)) _ & " ": GoTo Skip2
Numstring = ((Numstring / 10) - Fix(Numstring / 10)) * 10
If Numstring > 0 Then Tempstring = Tempstring & _ Number1s(CInt(Numstring)) & " " Skip2:
NumToWord = Tempstring
End Function
Public Sub initstrings() Steps(1) = "billion" Steps(2) = "million" Steps(3) = "thousand" Steps(4) = "hundred" Number1s(1) = "one" Number1s(2) = "two" Number1s(3) = "three" Number1s(4) = "four" Number1s(5) = "five" Number1s(6) = "six" Number1s(7) = "seven" Number1s(8) = "eight" Number1s(9) = "nine" Number1s(10) = "ten" Number1s(11) = "eleven" Number1s(12) = "twelve" Number1s(13) = "thirteen" Number1s(14) = "fourteen" Number1s(15) = "fifteen" Number1s(16) = "sixteen" Number1s(17) = "seventeen" Number1s(18) = "eighteen" Number1s(19) = "nineteen" Number2s(2) = "twenty" Number2s(3) = "thirty" Number2s(4) = "forty" Number2s(5) = "fifty" Number2s(6) = "sixty" Number2s(7) = "seventy" Number2s(8) = "eighty" Number2s(9) = "ninety"
End Sub
|
No responses found. Be the first to respond and make money from revenue sharing program.
|