I found what i was looking for. Thanks.
FUNCTION tomoney3
* Relevant to US currency
* Written by VFP megaguru Tom Rettig, developer of
* Tom Rettig's Office software.
* Published with permission from Tom Rettig.
* Thank you, Tom.
* Modified for VFP by Ellen Sander
* Syntax: tomoney([])
* Action: Converts a number to a string of words.
* Return: uppercase string representing the number parameter.
* : Length of return string varies.
* : "*****" if number to large to convert.
* : String enclosed in ( ) if negative number.
* Notes : Creates a public array num_words[37] unless
* : it already exists.
* : Pass no parameter to release the array when done.
* : Largest number is 999,999,999,999.99.
* Usage : Useful for writing checks. For example:
* : USE Pay_roll INDEX Name
* : SCAN
* : ? "Pay to the order of:", Name
* : ? Salary
* : ? tomoney(Salary)
* : ENDSCAN
* : = tomoney() && release public array
* :
* : Use LOWER() or PROPER() to change from uppercase.
* : For example: ? PROPER(tomoney(Salary))
*
PARAMETERS amount
IF PARAMETERS()==0
RELEASE num_words
RETURN ""
ENDIF
IF ABS(amount)>=10^12
RETURN "*****"
ENDIF
IF TYPE("num_words")=="U"
PUBLIC num_words[37]
num_words[ 1] = "ONE"
num_words[ 2] = "TWO"
num_words[ 3] = "THREE"
num_words[ 4] = "FOUR"
num_words[ 5] = "FIVE"
num_words[ 6] = "SIX"
num_words[ 7] = "SEVEN"
num_words[ 8] = "EIGHT"
num_words[ 9] = "NINE"
num_words[10] = "TEN"
num_words[11] = "ELEVEN"
num_words[12] = "TWELVE"
num_words[13] = "THIRTEEN"
num_words[14] = "FOURTEEN"
num_words[15] = "FIFTEEN"
num_words[16] = "SIXTEEN"
num_words[17] = "SEVENTEEN"
num_words[18] = "EIGHTEEN"
num_words[19] = "NINETEEN"
num_words[20] = "TWENTY"
num_words[21] = "THIRTY"
num_words[22] = "FORTY"
num_words[23] = "FIFTY"
num_words[24] = "SIXTY"
num_words[25] = "SEVENTY"
num_words[26] = "EIGHTY"
num_words[27] = "NINETY"
num_words[28] = "HUNDRED"
num_words[29] = "THOUSAND"
num_words[30] = "MILLION"
num_words[31] = "BILLION"
num_words[32] = "DOLLARS"
num_words[33] = "AND"
num_words[34] = "CENTS"
num_words[35] = "NO"
num_words[36] = SET("POINT") && decimal character
num_words[37] = "(MINUS" && negative number
ENDIF
*
LOCAL STR ,POS, MAG, PTR,LEN, RET, ISN, ISZ, IS_, ISP, CNT, CUR, WRD
str = LTRIM(STR(ABS(amount),15,2)) && parameter string
pos = AT(num_words[36],str)-1 && decimal point position
mag = CEILING(pos/3) && magnitude of dollars
ptr = MOD(pos,3)+IIF(MOD(pos,3)==0,3,0) && digit pointer
len = LEN(str) && string length
ret = "" && return string
STORE .T. TO isn,isz && isn is no total, isz is zero
STORE .F. TO is_,isp && is_ is a hyphen, isp is a space
FOR cnt = 1 TO len
cur = VAL(SUBSTR(str,cnt,1)) && current digit
wrd = "" && current word
isn = isn .AND. cur==0
* Build current word from current digit.
DO CASE
CASE ptr!=2 .AND. cur>0
* Ones and hundreds.
wrd = num_words[cur]
CASE cur==1 && and ptr==2
* Teens.
ptr = ptr-1
cnt = cnt+1
cur = VAL(SUBSTR(str,cnt,1)) + 1
wrd = num_words[cur+9]
OTHERWISE && ptr==2 and cur!=1
* Tens.
is_ = cur>0
IF is_
wrd = num_words[cur+18]
ENDIF
ENDCASE
IF ptr==1 .AND. is_
IF cur>0
* Hyphenate tens.
wrd = "-" + wrd
ENDIF
STORE .F. TO is_, isp
ENDIF
* Build return string with current digit word.
IF !EMPTY(wrd)
ret = ret + IIF(isp," ","") + wrd
ENDIF
isz = isz .AND. cur==0
* Add magnitude word.
DO CASE
CASE ptr==3 .AND. !isz && "HUNDRED"
ret = ret +" "+ num_words[28]
CASE ptr==1 .AND. mag>1
IF !isz .AND. mag>=2
* "THOUSAND", "MILLION", "BILLION"
ret = ret +" "+ num_words[mag+27]
ENDIF
mag = mag-1
isz = .T.
ENDCASE
* Add currency words.
IF cnt!=len .AND. SUBSTR(str,cnt+1,1)==num_words[36]
IF EMPTY(ret)
ret = num_words[35] && "NO"
ENDIF
ret = ret +" "+ IIF(ret==num_words[1],;
LEFT(num_words[32],6),;
num_words[32]) ;
+" "+ num_words[33] && "DOLLARS AND"
ptr = 2
cnt = cnt+1
isz = .T.
ELSE
IF cnt==len && "CENTS"
ret = ret+IIF(isz, " "+num_words[35], "")+" "+;
IIF(RIGHT(ret,4)==" "+num_words[1],;
LEFT(num_words[34],4),;
num_words[34])
ENDIF
ptr = IIF(ptr==1, 3, ptr-1)
ENDIF
isp = .T.
ENDFOR
IF amount<0
ret = num_words[37] + ret + ")"
ENDIF
RETURN ret
Previous
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only