?Num2Words(987654321789.456)
*?Num2Words(-1.00)
*?Num2Words(0.00)
Function Num2Words(lnNumeral, lcCurrency)
Local lnInteger, lnDecimal, lcNum2Str, lcString1, lcString2
Local lcString3, lcReturnStr, lnX, lnWidthStr, lcSegment, lnIncrement
Local lcCluster, lnOrigLen
If Type('lnNumeral') # "N"
Return ""
Endif
lcCurrency = IIf(Type('lcCurrency') # "C", "Dollar(s)", lcCurrency)
lnInteger = Int(lnNumeral)
lnDecimal = lnNumeral - lnInteger
*!* If lnInteger = 0 And lnDecimal = 0
*!* Return "Zero"
*!* Endif
lcNum2Str=Alltrim(Str(lnInteger, 237)) && AllTrim(BetterStr(lnInteger))
Store Len(lcNum2Str) To lnWidthStr, lnOrigLen
If !Between(lnInteger, -999999999999, 999999999999) && -999999999999.99, 999999999999.99)
Return Replicate('*', lnWidthStr) &&Len(lcNum2Str))
Endif
lcString1 = " One Two ThreeFour Five Six SevenEightNine "
*___________"----5---10---15---20---25---30---35---40---45---50"
lcString2 = "Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen SeventeenEighteen Nineteen "
*___________"--------9-------18-------27-------36-------45-------54-------63-------72-------81-------90"
lcString3 = " Twenty Thirty Forty Fifty Sixty SeventyEighty Ninety "
*___________"------7-----14-----21-----28-----35-----42-----49-----56-----63"
*Store Len(lcNum2Str) To lnWidthStr, lnOrigLen
lcReturnStr = IIf(lnInteger < 0, "Negative ", "")
Do While lnWidthStr > 0 And lnInteger # 0
lnX = Mod(lnWidthStr, 3)
lnX = IIf(lnX = 0, 3, lnX)
lcCluster = AllTrim(Left(lcNum2Str, lnX))
For lnIncrement = 1 To lnX
lcSegment = AllTrim(SubStr(lcCluster, lnIncrement, 3))
If Len(lcSegment) # 2
lcReturnStr = AllTrim(lcReturnStr) + " " + ;
AllTrim(SubStr(lcString1, Val(Left(lcSegment,1)) * 5 + 1,5))
If Len(lcSegment) = 3 And Val(Left(lcSegment, 1)) > 0
lcReturnStr = AllTrim(lcReturnStr) + " Hundred"
Endif
Else
If Val(Left(lcSegment,1)) < 2
If Val(Left(lcSegment,2)) > 9
lcReturnStr = AllTrim(lcReturnStr) + " " + ;
AllTrim(SubStr(lcString2, Val(Right(lcSegment,1)) * 9 + 1,9))
Exit
Endif
Else
lcReturnStr = AllTrim(lcReturnStr) + " " + ;
AllTrim(SubStr(lcString3, (Val(Left(lcSegment,1))-1) * 7 + 1,7))
If Right(lcSegment,1) = "0"
Exit
Endif
Endif
Endif
Next
Do Case
Case Len(lcNum2Str) > 9 And Val(lcCluster) # 0
lcReturnStr = AllTrim(lcReturnStr) + " Billion"
Case Len(lcNum2Str) > 6 And Val(lcCluster) # 0
lcReturnStr = AllTrim(lcReturnStr) + " Million"
Case Len(lcNum2Str) > 3 And Val(lcCluster) # 0
lcReturnStr = AllTrim(lcReturnStr) + " Thousand"
Endcase
lnWidthStr = lnWidthStr - lnX
lcNum2Str = Right(lcNum2Str, lnWidthStr)
Enddo
lcReturnStr = AllTrim(lcReturnStr) + " " + IIf(Empty(lcReturnStr), "", lcCurrency)
*!* If lnDecimal # 0.00
lcReturnStr = lcReturnStr + IIf(lnInteger = 0 And Empty(AllTrim(lcReturnStr)), "Negative ", " And ")
lcReturnStr = lcReturnStr + AllTrim(Str(Abs(lnDecimal * 100), 2, 2)) + "/100 Cents"
*!* Endif
Return AllTrim(lcReturnStr)