rrussell1029
Programmer
- Mar 12, 2009
- 3
Hi, I'm kind of new to VFP and I have a question. The guy who worked my job before me left a program that my boss said would turn a given number into a text field format, like what you would see on a check, IE 100597.65 becomes "One Hundred Thousand Five Hundred Ninety Seven dollars and 65/100". The problem is, neither of us can seem to pass a numeric field to it without it giving an error of "invalid subscript reference". I'm not too good at troubleshooting code, so I was hoping you guys could give me a hand. If someone could tell me what exactly I have to do to get this to work, it would be awesome, thanks! I'll post the code below so you can go through it.
Program:
******************************************************************************************
**
** PROGRAM: MTOC
** PURPOSE: Convert Currency of Numeric value to English Words.
**
** ENHANCEMENTS:
** 1. Comment Logic
**
******************************************************************************************
LPARAMETERS tNumToConvert
* Variable(s) & Array(s)
LOCAL NumToConvert,Results,Temp
LOCAL cDollars,cCents
LOCAL nDecimalPlace,nCount
STORE "" TO NumToConvert,Results,cDollars,cCents
STORE 0 TO nDecimalPlace
STORE 1 TO nCount
LOCAL ARRAY aPlace[9]
STORE "" TO aPlace
aPlace[2] = " Thousand "
aPlace[3] = " Million "
aPlace[4] = " Billion "
aPlace[5] = " Trillion "
aPlace[6] = " Quadrillion "
aPlace[7] = " Quintillion "
aPlace[8] = " Sextillion "
aPlace[9] = " Septillion "
* Convert Input to String
DO CASE
CASE EMPTY(tNumToConvert)
RETURN "0"
CASE INLIST(TYPE("tNumToConvert"),"N","Y")
IF tNumToConvert > 9999999999999.99
RETURN "**,***,***,***,***.**"
ENDIF
NumToConvert = ALLT(STR(ABS(tNumToConvert),16,2))
OTHERWISE
NumToConvert = IIF(LEFT(ALLT(tNumToConvert),1)=="-",SUBSTR(ALLT(tNumToConvert),2),ALLT(tNumToConvert))
ENDCASE
* Convert Cents
nDecimalPlace = AT(".",NumToConvert)
IF nDecimalPlace>0 THEN
cCents = ConvertTens(LEFT(SUBSTR(NumToConvert,nDecimalPlace+1)+"00",2))
NumToConvert = ALLT(LEFT(NumToConvert,nDecimalPlace-1))
ENDIF
DO CASE
CASE cCents==""
cCents = " and no/100"
CASE cCents=="One"
cCents = " and 01/100"
OTHERWISE
cCents = " and " + cCents + "/100"
ENDCASE
* Convert Dollars
DO WHILE !EMPTY(NumToConvert)
Temp = ConvertHundreds(RIGHT(NumToConvert,3))
IF !EMPTY(Temp) THEN
cDollars = Temp+aPlace[nCount]+cDollars
ENDIF
IF LEN(NumToConvert)>3 Then
NumToConvert = LEFT(NumToConvert,LEN(NumToConvert)-3)
ELSE
NumToConvert = ""
ENDIF
nCount = nCount + 1
ENDDO
*!* DO CASE
*!* CASE cDollars==""
*!* cDollars = "No Dollars"
*!* CASE cDollars=="One"
*!* cDollars = "One Dollar"
*!* OTHERWISE
*!* cDollars = cDollars + " Dollars"
*!* ENDCASE
RETURN cDollars + cCents
FUNCTION ConvertHundreds
LPARAMETERS NumToConvert
LOCAL Result
Result = ""
IF VAL(NumToConvert)==0 THEN
RETURN Result
ENDIF
NumToConvert = PADL(NumToConvert,3,"0")
IF LEFT(NumToConvert,1)#"0" THEN
Result = ConvertDigit(LEFT(NumToConvert,1))+" Hundred "
ENDIF
IF SUBSTR(NumToConvert,2,1)#"0" THEN
Result = Result+ConvertTens(SUBSTR(NumToConvert,2))
ELSE
Result = Result+ConvertDigit(SUBSTR(NumToConvert,3))
ENDIF
ConvertHundreds = ALLTRIM(Result)
RETURN ConvertHundreds
ENDFUNC
FUNCTION ConvertTens
LPARAMETERS MyTens
LOCAL Result
Result = ""
IF VAL(LEFT(MyTens,1))==1 THEN
DO CASE
CASE VAL(MyTens)==10
Result = "Ten"
CASE VAL(MyTens)==11
Result = "Eleven"
CASE VAL(MyTens)==12
Result = "Twelve"
CASE VAL(MyTens)==13
Result = "Thirteen"
CASE VAL(MyTens)==14
Result = "Fourteen"
CASE VAL(MyTens)==15
Result = "Fifteen"
CASE VAL(MyTens)==16
Result = "Sixteen"
CASE VAL(MyTens)==17
Result = "Seventeen"
CASE VAL(MyTens)==18
Result = "Eighteen"
CASE VAL(MyTens)==19
Result = "Nineteen"
ENDCASE
ELSE
DO CASE
CASE VAL(LEFT(MyTens,1))==2
Result = "Twenty "
CASE VAL(LEFT(MyTens,1))==3
Result = "Thirty "
CASE VAL(LEFT(MyTens,1))==4
Result = "Forty "
CASE VAL(LEFT(MyTens,1))==5
Result = "Fifty "
CASE VAL(LEFT(MyTens,1))==6
Result = "Sixty "
CASE VAL(LEFT(MyTens,1))==7
Result = "Seventy "
CASE VAL(LEFT(MyTens,1))==8
Result = "Eighty "
CASE VAL(LEFT(MyTens,1))==9
Result = "Ninety "
ENDCASE
Result = Result+ConvertDigit(RIGHT(MyTens,1))
ENDIF
ConvertTens = Result
RETURN ConvertTens
ENDFUNC
FUNCTION ConvertDigit
LPARAMETERS MyDigit
DO CASE
CASE VAL(MyDigit)==1
ConvertDigit = "One"
CASE VAL(MyDigit)==2
ConvertDigit = "Two"
CASE VAL(MyDigit)==3
ConvertDigit = "Three"
CASE VAL(MyDigit)==4
ConvertDigit = "Four"
CASE VAL(MyDigit)==5
ConvertDigit = "Five"
CASE VAL(MyDigit)==6
ConvertDigit = "Six"
CASE VAL(MyDigit)==7
ConvertDigit = "Seven"
CASE VAL(MyDigit)==8
ConvertDigit = "Eight"
CASE VAL(MyDigit)==9
ConvertDigit = "Nine"
OTHERWISE
ConvertDigit = ""
ENDCASE
RETURN ConvertDigit
ENDFUNC
Program:
******************************************************************************************
**
** PROGRAM: MTOC
** PURPOSE: Convert Currency of Numeric value to English Words.
**
** ENHANCEMENTS:
** 1. Comment Logic
**
******************************************************************************************
LPARAMETERS tNumToConvert
* Variable(s) & Array(s)
LOCAL NumToConvert,Results,Temp
LOCAL cDollars,cCents
LOCAL nDecimalPlace,nCount
STORE "" TO NumToConvert,Results,cDollars,cCents
STORE 0 TO nDecimalPlace
STORE 1 TO nCount
LOCAL ARRAY aPlace[9]
STORE "" TO aPlace
aPlace[2] = " Thousand "
aPlace[3] = " Million "
aPlace[4] = " Billion "
aPlace[5] = " Trillion "
aPlace[6] = " Quadrillion "
aPlace[7] = " Quintillion "
aPlace[8] = " Sextillion "
aPlace[9] = " Septillion "
* Convert Input to String
DO CASE
CASE EMPTY(tNumToConvert)
RETURN "0"
CASE INLIST(TYPE("tNumToConvert"),"N","Y")
IF tNumToConvert > 9999999999999.99
RETURN "**,***,***,***,***.**"
ENDIF
NumToConvert = ALLT(STR(ABS(tNumToConvert),16,2))
OTHERWISE
NumToConvert = IIF(LEFT(ALLT(tNumToConvert),1)=="-",SUBSTR(ALLT(tNumToConvert),2),ALLT(tNumToConvert))
ENDCASE
* Convert Cents
nDecimalPlace = AT(".",NumToConvert)
IF nDecimalPlace>0 THEN
cCents = ConvertTens(LEFT(SUBSTR(NumToConvert,nDecimalPlace+1)+"00",2))
NumToConvert = ALLT(LEFT(NumToConvert,nDecimalPlace-1))
ENDIF
DO CASE
CASE cCents==""
cCents = " and no/100"
CASE cCents=="One"
cCents = " and 01/100"
OTHERWISE
cCents = " and " + cCents + "/100"
ENDCASE
* Convert Dollars
DO WHILE !EMPTY(NumToConvert)
Temp = ConvertHundreds(RIGHT(NumToConvert,3))
IF !EMPTY(Temp) THEN
cDollars = Temp+aPlace[nCount]+cDollars
ENDIF
IF LEN(NumToConvert)>3 Then
NumToConvert = LEFT(NumToConvert,LEN(NumToConvert)-3)
ELSE
NumToConvert = ""
ENDIF
nCount = nCount + 1
ENDDO
*!* DO CASE
*!* CASE cDollars==""
*!* cDollars = "No Dollars"
*!* CASE cDollars=="One"
*!* cDollars = "One Dollar"
*!* OTHERWISE
*!* cDollars = cDollars + " Dollars"
*!* ENDCASE
RETURN cDollars + cCents
FUNCTION ConvertHundreds
LPARAMETERS NumToConvert
LOCAL Result
Result = ""
IF VAL(NumToConvert)==0 THEN
RETURN Result
ENDIF
NumToConvert = PADL(NumToConvert,3,"0")
IF LEFT(NumToConvert,1)#"0" THEN
Result = ConvertDigit(LEFT(NumToConvert,1))+" Hundred "
ENDIF
IF SUBSTR(NumToConvert,2,1)#"0" THEN
Result = Result+ConvertTens(SUBSTR(NumToConvert,2))
ELSE
Result = Result+ConvertDigit(SUBSTR(NumToConvert,3))
ENDIF
ConvertHundreds = ALLTRIM(Result)
RETURN ConvertHundreds
ENDFUNC
FUNCTION ConvertTens
LPARAMETERS MyTens
LOCAL Result
Result = ""
IF VAL(LEFT(MyTens,1))==1 THEN
DO CASE
CASE VAL(MyTens)==10
Result = "Ten"
CASE VAL(MyTens)==11
Result = "Eleven"
CASE VAL(MyTens)==12
Result = "Twelve"
CASE VAL(MyTens)==13
Result = "Thirteen"
CASE VAL(MyTens)==14
Result = "Fourteen"
CASE VAL(MyTens)==15
Result = "Fifteen"
CASE VAL(MyTens)==16
Result = "Sixteen"
CASE VAL(MyTens)==17
Result = "Seventeen"
CASE VAL(MyTens)==18
Result = "Eighteen"
CASE VAL(MyTens)==19
Result = "Nineteen"
ENDCASE
ELSE
DO CASE
CASE VAL(LEFT(MyTens,1))==2
Result = "Twenty "
CASE VAL(LEFT(MyTens,1))==3
Result = "Thirty "
CASE VAL(LEFT(MyTens,1))==4
Result = "Forty "
CASE VAL(LEFT(MyTens,1))==5
Result = "Fifty "
CASE VAL(LEFT(MyTens,1))==6
Result = "Sixty "
CASE VAL(LEFT(MyTens,1))==7
Result = "Seventy "
CASE VAL(LEFT(MyTens,1))==8
Result = "Eighty "
CASE VAL(LEFT(MyTens,1))==9
Result = "Ninety "
ENDCASE
Result = Result+ConvertDigit(RIGHT(MyTens,1))
ENDIF
ConvertTens = Result
RETURN ConvertTens
ENDFUNC
FUNCTION ConvertDigit
LPARAMETERS MyDigit
DO CASE
CASE VAL(MyDigit)==1
ConvertDigit = "One"
CASE VAL(MyDigit)==2
ConvertDigit = "Two"
CASE VAL(MyDigit)==3
ConvertDigit = "Three"
CASE VAL(MyDigit)==4
ConvertDigit = "Four"
CASE VAL(MyDigit)==5
ConvertDigit = "Five"
CASE VAL(MyDigit)==6
ConvertDigit = "Six"
CASE VAL(MyDigit)==7
ConvertDigit = "Seven"
CASE VAL(MyDigit)==8
ConvertDigit = "Eight"
CASE VAL(MyDigit)==9
ConvertDigit = "Nine"
OTHERWISE
ConvertDigit = ""
ENDCASE
RETURN ConvertDigit
ENDFUNC