Here is an Xbasic function based on VB code I found on the web. It is used to write the amount in words on cheques.
From the Interactive window:
?spellnumber(11223344.55)
= "Eleven Million Two Hundred Twenty Three Thousand Three Hundred Forty Four Dollars and Fifty Five Cents"
Garry
Code:
FUNCTION SpellNumber AS C (InNum as n) Dim Dollars as c dim Cents as c dim Temp as c = "" Dim DecimalPlace as n dim Count as n = 1 dim MyNumber as c Dim Place[1..9] As c Place[1] = "" Place[2] = " Thousand " Place[3] = " Million " Place[4] = " Billion " Place[5] = " Trillion " ' String representation of amount. MyNumber = alltrim(str(InNum,15,2,"B")) ' Position of decimal place 0 if none. DecimalPlace = at("." ,MyNumber) ' Convert cents and set MyNumber to dollar amount. IF DecimalPlace > 0 THEN Cents = GetTens( substr(MyNumber,DecimalPlace+1, 2)) MyNumber = substr(MyNumber,1,DecimalPlace - 1) END IF While MyNumber <> "" Temp = GetHundreds(Right(MyNumber, 3)) If Temp <> "" Then Dollars = Temp + Place[Count] + Dollars end if If Len(MyNumber) > 3 Then MyNumber = Left(MyNumber, Len(MyNumber) - 3) Else MyNumber = "" End If 'debug(1) Count = Count + 1 end while SELECT CASE Dollars = "" Dollars = "No Dollars" CASE Dollars = "One" Dollars = "One Dollar" CASE Else Dollars = Dollars + " Dollars" END SELECT 'end if SELECT CASE cents = "" Cents = " and No Cents" CASE cents = "One" Cents = " and One Cent" CASE Else Cents = " and " + Cents + " Cents" END SELECT 'debug(1) SpellNumber = Dollars + Cents END FUNCTION FUNCTION GetHundreds as c (HunNum as c) Dim Result As c If Val(HunNum) = 0 Then exit Function end if if len(HunNum) = 3 then result = GetDigit(substr(HunNum,1,1)) + " Hundred " Result = Result + GetTens(substr(HunNum,2,2)) end if IF len(HunNum)=2 THEN Result = Result + GetTens(HunNum) ELSE Result = Result + GetDigit(HunNum) END IF GetHundreds = Result END FUNCTION '********************************************* ' Converts a number from 10 to 99 into text. * '********************************************* FUNCTION GetTens(TensText) Dim Result As c Result = "" ' Null out the temporary function value. IF Val(Left(TensText, 1)) = 1 THEN ' IF value between 10-19... SELECT CASE Val(TensText) = 10 Result = "Ten" CASE Val(TensText) = 11 Result = "Eleven" CASE Val(TensText) = 12 Result = "Twelve" CASE Val(TensText) = 13 Result = "Thirteen" CASE Val(TensText) = 14 Result = "Fourteen" CASE Val(TensText) = 15 Result = "Fifteen" CASE Val(TensText) = 16 Result = "Sixteen" CASE Val(TensText) = 17 Result = "Seventeen" CASE Val(TensText) = 18 Result = "Eighteen" CASE Val(TensText) = 19 Result = "Nineteen" CASE Else END SELECT ELSE ' IF value between 20-99... SELECT CASE Val(substr(TensText,1,1)) = 2 Result = "Twenty " CASE Val(substr(TensText,1,1)) = 3 Result = "Thirty " CASE Val(substr(TensText,1,1)) = 4 Result = "Forty " CASE Val(substr(TensText,1,1)) = 5 Result = "Fifty " CASE Val(substr(TensText,1,1)) = 6 Result = "Sixty " CASE Val(substr(TensText,1,1)) = 7 Result = "Seventy " CASE Val(substr(TensText,1,1)) = 8 Result = "Eighty " CASE Val(substr(TensText,1,1)) = 9 Result = "Ninety " CASE Else END SELECT Result = Result + GetDigit(substr(TensText,2,1)) ' Retrieve ones place. END IF GetTens = Result END FUNCTION '******************************************* ' Converts a number from 1 to 9 into text. * '******************************************* FUNCTION GetDigit as c (Digit as c) SELECT CASE Val(Digit) = 1 GetDigit = "One" CASE Val(Digit) = 2 GetDigit = "Two" CASE Val(Digit) = 3 GetDigit = "Three" CASE Val(Digit) = 4 GetDigit = "Four" CASE Val(Digit) = 5 GetDigit = "Five" CASE Val(Digit) = 6 GetDigit = "Six" CASE Val(Digit) = 7 GetDigit = "Seven" CASE Val(Digit) = 8 GetDigit = "Eight" CASE Val(Digit) = 9 GetDigit = "Nine" CASE Else GetDigit = "" END SELECT END FUNCTION
?spellnumber(11223344.55)
= "Eleven Million Two Hundred Twenty Three Thousand Three Hundred Forty Four Dollars and Fifty Five Cents"
Garry
Comment