Number To Words

Category: Strings

Date: 02-16-2022

Return to Index


 
 
 
'Compilable Example:  (Jose Includes)
#Compile Exe
#Dim All
#Include "Win32API.inc"
Global hDlg as Dword
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"Push", 50,10,100,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
      SayText (NumberToWords(1156.28))
   End If
End Function
 
Sub SayText (sText As WStringZ*%Max_Path)
   Local vRes, vTxt, vTime As Variant, oSp As Dispatch
   Let oSp = NewCom "SAPI.SpVoice"
   If IsFalse IsObject(oSp) Then Exit Sub
   vTxt = sText
   Object Call oSp.Speak(vTxt) To vRes
   vTime = -1 As Long
   Object Call oSp.WaitUntilDone(vTime) To vRes
End Sub
 
Function NumberToWords(ByVal Number As Currency) As String
   Static Level      As Dword
   Local  Negative, dwCent   As Dword, sCent As String
 
   Incr Level
   If Level = 1 Then
      If Number < -922337203685477 Then 'If number is to big it will revert to -922337203685478
         NumberToWords = "ERROR"
         Level = 0
         Exit Function
      End If
      If Number < 0 Then Negative = %TRUE
      dwCent = Abs(Frac(Number)) * 100
      sCent = " and " & Format$(Abs(dwCent),"00") & " CENT" & IIf$(dwCent > 1, "S ", "")
      sCent = "DOLLAR" & IIf$(Number <= -2 Or Number >= 2, "S", "") & sCent
      Number = Fix(Number)
   End If
 
   Select Case Number
      Case < 0
         Number = Abs(Number)
         NumberToWords = "(MINUS) " & NumberToWords(Number) & sCent
      Case 0
         If Level = 1 Then NumberToWords = IIf$(Negative, "(MINUS) ", "") & "ZERO " & sCent
      Case < 20 '1 TO 19
         NumberToWords = Choose$(Number, "ONE ", "TWO ", "THREE ", "FOUR " , "FIVE ", "SIX ", _
            "SEVEN ", "EIGHT ", "NINE ", "TEN ", "ELEVEN ", "TWELVE ", "THIRTEEN ", "FOURTEEN ", _
            "FIFTEEN ", "SIXTEEN ", "SEVENTEEN ", "EIGHTEEN ", "NINETEEN ") & sCent
      Case < 100 '20 TO 99
         NumberToWords = Choose$(Number \ 10 - 1, "TWENTY", "THIRTY", "FORTY", _
            "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY") & _
            IIf$(Number Mod 10 <> 0, "-", " ") & NumberToWords(Number Mod 10) & sCent
      Case < 1000 '100 TO 999
         NumberToWords = NumberToWords(Number \ 100) & "HUNDRED " & NumberToWords(Number Mod 100) & sCent
      Case < 1000000 '1000 TO 999999
         NumberToWords = NumberToWords(Number \ 1000) & "THOUSAND " & NumberToWords(Number Mod 1000) & sCent
      Case < 1000000000 '1000000 TO 999999999
         NumberToWords = NumberToWords(Number \ 1000000) & "MILLION " & NumberToWords(Number Mod 1000000) & sCent
      Case < 1000000000000 '1000000000 TO 999999999999
         NumberToWords = NumberToWords(Number \ 1000000000) & "BILLION " & NumberToWords(Number Mod 1000000000) & sCent
      Case <= 922337203685477
         NumberToWords = NumberToWords(Number \ 1000000000000) & "TRILLION " & NumberToWords(Number Mod 1000000000000) & sCent
   End Select
   Decr Level
End Function 
   
'gbs_01363
'Date: 05-11-2013


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm