Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'Pierre: http://www.powerbasic.com/support/pbforums/showthread.php?t=23535
'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
NumberToWords 1151.22
End If
End Function
Function NumberToWords(ByVal Number As Currency)
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", "FOURTY", _
"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_01376
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm