;==============================================================================================================================================================
; udfNumberToWords (Amount, FirstCharUp, Sign, Fraction, Delimiter, Currency) ; Returns string for mortgage or financial purposes
; udfNumberToShortWords (Number, Delimiter)
;==============================================================================================================================================================
; Detlev Dalitz.20010325.20010621.20010717
;==============================================================================================================================================================
;--------------------------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfsaynoyes",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfsaynoyes
#DefineFunction udfSayNoYes(bool) ; returns string "No" or "Yes" ; for test purposes
NoYesArray = Arrayize("Nein,Ja",",")
;NoYesArray = Arrayize("No,Yes",",")
Return (NoYesArray[bool])
#EndFunction
:skip_udfsaynoyes
;--------------------------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfnumbertoshortwords",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfnumbertoshortwords
#DefineFunction udfNumberToShortWords (Number, Delimiter)
If (number=="") Then Return ("")
If !IsNumber(number) Then Return ("")
NumberIsNegative = (Number<0)
NumberArr = Arrayize("zero|one|two|three|four|five|six|seven|eight|nine","|")
DecimalPoint = "."
DecimalText = "point"
Minus = "negative"
num1 = ItemExtract(1,Number,DecimalPoint)
num2 = ItemExtract(2,Number,DecimalPoint)
len1 = StrLen(num1)
len2 = StrLen(num2)
NumberStr = ""
If NumberIsNegative Then NumberStr = ItemInsert(Minus,1,NumberStr,Delimiter)
For i=1+NumberIsNegative To len1
NumberStr = ItemInsert(NumberArr[StrSub(num1,i,1)],-1,NumberStr,Delimiter)
Next
If (len2 > 0)
NumberStr = ItemInsert(DecimalText,-1,NumberStr,Delimiter)
For i=1 To len2
NumberStr = ItemInsert(NumberArr[StrSub(num2,i,1)],-1,NumberStr,Delimiter)
Next
EndIf
NumberStr = ItemInsert(StrCat(Delimiter,Delimiter),0,NumberStr,Delimiter)
NumberStr = ItemInsert(StrCat(Delimiter,Delimiter),-1,NumberStr,Delimiter)
Return (numberstr)
; For using german language replace statements above
; NumberArr = Arrayize("null|eins|zwei|drei|vier|fuenf|sechs|sieben|acht|neun","|") ; german
; DecimalPoint = ","
; DecimalText = "komma"
; Minus = "minus"
#EndFunction
:skip_udfnumbertoshortwords
;--------------------------------------------------------------------------------------------------------------------------------------------------------------
If (ItemLocate("udfntowrecursion",IntControl(77,103,0,0,0),@TAB)>0) Then Goto skip_udfntowrecursion
#DefineFunction udfNtoWRecursion(R, M, WW, EW, DW, Delimiter)
If (R==0) Then Return (WW[0])
t=""
If (R>=1000) Then t=StrCat(udfNtoWRecursion(R/1000,M+1,WW,EW,DW,Delimiter),StrCat(WW[2+M],Delimiter))
R=R mod 1000
h=R/100
d=(R mod 100)/10
e=R mod 10
If (h>0)
t=StrCat(t,EW[h],Delimiter)
t=StrCat(t,WW[1],Delimiter)
EndIf
If ((d<=1)&&(e>0))
t=StrCat(t,EW[(d*10)+e])
If ((d==0)&&(e==1)&&(M==0)) Then t=StrCat(t,"s")
t=StrCat(t,Delimiter)
Else
If (e>0) Then t=StrCat(t,EW[e],Delimiter);
If ((d*e)>0) Then t=StrCat(t,WW[4],Delimiter);
If (d>0) Then t=StrCat(t,DW[d],Delimiter);
EndIf
Return (t)
#EndFunction
:skip_udfntowrecursion
;--------------------------------------------------------------------------------------------------------------------------------------------------------------
If (ItemLocate("udfnumbertowords",IntControl(77,103,0,0,0),@TAB)>0) Then Goto skip_udfnumbertowords
#DefineFunction udfNumberToWords (Amount, FirstCharUp, Sign, Fraction, Delimiter, Currency)
; returns string for mortgage or financial purposes
; FirstCharUp=0=no ; FirstCharUp=1=yes
; Sign=0=no ; Sign=1=yes if negative
; Fraction=0=no ; Fraction=1=yes auto-zerosuppress
; Delimiter=String e.g. " " or "" or "_"
; Currency=String e.g. "DM" or "USD" or "US-Dollar or "Euro"
;
WW = ArrDimension(5)
Arr = Arrayize("null,hundert,tausend,millionen,und",",")
For i=0 To 4
WW[i] = Arr[i]
Next
Drop(Arr)
EW = ArrDimension(20)
Arr = Arrayize(",ein,zwei,drei,vier,fuenf,sechs,sieben,acht,neun,zehn,elf,zwoelf,dreizehn,vierzehn,fuenfzehn,sechzehn,siebzehn,achtzehn,neunzehn",",")
For i=0 To 19
EW[i] = Arr[i]
Next
Drop(Arr)
DW = ArrDimension(10)
Arr = Arrayize(",zehn,zwanzig,dreissig,vierzig,fuenfzig,sechzig,siebzig,achtzig,neunzig",",")
For i=0 To 9
DW[i] = Arr[i]
Next
Drop(Arr)
CurrencyExist = (Currency<>"")
AmountIsNegative = (Amount<0)
Amount = Fabs(Amount)
AmountFloor = Int(Floor(Amount))
Dividend = Amount - AmountFloor
Dividend = ItemExtract(2,Dividend,".")
Divisor = 100
NumberStr = udfNtoWRecursion(AmountFloor, 0, WW, EW, DW, Delimiter)
NumberStrLen = StrLen(NumberStr)
If (StrSub(NumberStr,NumberStrLen,1)==Delimiter)
NumberStr = StrSub(NumberStr,1,NumberStrLen-1) ; remove last Delimiter
EndIf
If (FirstCharUp||Sign||Fraction||CurrencyExist)
Select 1
Case FirstCharUp
NumberStr = StrCat(StrUpper(StrSub(NumberStr,1,1)), StrSub(NumberStr,2,-1))
Continue
Case Sign
If AmountIsNegative Then NumberStr = StrCat("minus*",NumberStr)
Continue
Case CurrencyExist
NumberStr = StrCat (NumberStr,"*",Currency)
Continue
Case Fraction
If (Dividend>0) Then NumberStr = StrCat(NumberStr,"*",Dividend,"/",Divisor)
Continue
EndSelect
NumberStr = StrCat("***",NumberStr,"***")
EndIf
Return (NumberStr)
#EndFunction
:skip_udfnumbertowords
;--------------------------------------------------------------------------------------------------------------------------------------------------------------
; --- test ---
oldDecimals = Decimals(2)
While 1
OutStr = ""
; generate Testnumber
BetragAlt = 0
While @TRUE
Betrag = 1.0 * Random(999) * Random(999)* Random(999) * (Random(2) - 1) / 100
If (Betrag>=1E9) Then Continue
If (BetragAlt==Betrag) Then Continue
BetragAlt = Betrag
Break
EndWhile
OutStr = StrCat(OutStr,"Betrag = ",Betrag,@CRLF,@CRLF,@CRLF)
:test1
OutStr = StrCat(OutStr,"NumberToShortWords (Number, Delimiter)",@CRLF)
zeile = udfNumberToShortWords(Betrag,"*")
OutStr = StrCat(OutStr,@CRLF,zeile,@CRLF,@CRLF,@CRLF)
:test2
OutStr = StrCat(OutStr,"udfNumberToWords (Amount, FirstCharUp, Sign, Fraction, Delimiter, Currency)",@CRLF,@CRLF)
FirstCharUp=1
Sign=1
Fraction=1
Delimiter="|"
Currency="DM"
zeile = udfNumberToWords(Betrag,FirstCharUp,Sign,Fraction,Delimiter,Currency)
OutStr = StrCat(OutStr,'FirstCharUp=',udfSayNoYes(FirstCharUp),', Sign=',udfSayNoYes(Sign),', Fraction=',udfSayNoYes(Fraction))
OutStr = StrCat(OutStr,', Delimiter="',Delimiter,'", Currency="',Currency,'"',@CRLF,zeile,@CRLF,@CRLF)
FirstCharUp=@TRUE
Sign=0
Fraction=@TRUE
Delimiter=" "
Currency="DM"
zeile = udfNumberToWords(Betrag,FirstCharUp,Sign,Fraction,Delimiter,Currency)
OutStr = StrCat(OutStr,'FirstCharUp=',udfSayNoYes(FirstCharUp),', Sign=',udfSayNoYes(Sign),', Fraction=',udfSayNoYes(Fraction))
OutStr = StrCat(OutStr,', Delimiter="',Delimiter,'", Currency="',Currency,'"',@CRLF,zeile,@CRLF,@CRLF)
FirstCharUp=@TRUE
Sign=@TRUE
Fraction=@FALSE
Delimiter=""
Currency="US-Dollar"
zeile = udfNumberToWords(Betrag,FirstCharUp,Sign,Fraction,Delimiter,Currency)
OutStr = StrCat(OutStr,'FirstCharUp=',udfSayNoYes(FirstCharUp),', Sign=',udfSayNoYes(Sign),', Fraction=',udfSayNoYes(Fraction))
OutStr = StrCat(OutStr,', Delimiter="',Delimiter,'", Currency="',Currency,'"',@CRLF,zeile,@CRLF,@CRLF)
FirstCharUp=@FALSE
Sign=@TRUE
Fraction=@TRUE
Delimiter=""
Currency="Euro"
zeile = udfNumberToWords(Betrag,FirstCharUp,Sign,Fraction,Delimiter,Currency)
OutStr = StrCat(OutStr,'FirstCharUp=',udfSayNoYes(FirstCharUp),', Sign=',udfSayNoYes(Sign),', Fraction=',udfSayNoYes(Fraction))
OutStr = StrCat(OutStr,', Delimiter="',Delimiter,'", Currency="',Currency,'"',@CRLF,zeile,@CRLF,@CRLF)
FirstCharUp=@FALSE
Sign=@FALSE
Fraction=@FALSE
Delimiter=""
Currency=""
zeile = udfNumberToWords(Betrag,FirstCharUp,Sign,Fraction,Delimiter,Currency)
OutStr = StrCat(OutStr,'FirstCharUp=',udfSayNoYes(FirstCharUp),', Sign=',udfSayNoYes(Sign),', Fraction=',udfSayNoYes(Fraction))
OutStr = StrCat(OutStr,', Delimiter="',Delimiter,'", Currency="',Currency,'"',@CRLF,zeile,@CRLF,@CRLF)
Pause("Zahlen in Worte",OutStr)
EndWhile
:CANCEL
Decimals(oldDecimals)
Exit
;--------------------------------------------------------------------------------------------------------------------------------------------------------------
|