Microsoft KB Archive/259663

= How To Convert Currency or Numbers into English Words =

Article ID: 259663

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 3.0b for Macintosh

-



This article was previously published under Q259663



SUMMARY
This article shows you how to create a sample, user-defined function named ConvertCurrencyToEnglish to convert a numeric value to an English word representation. For example, the function returns the following words for the number 1234.56:

One Thousand Two Hundred Thirty Four Dollars And Fifty Six Cents



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

To create the ConvertCurrencyToEnglish function, follow these steps:  Create a new program and save it as ConvertCurrencyToEnglish:  Copy and past the following code to the new program: *FUNCTION ConvertCurrencyToEnglish LPARAMETERS tNumToConvert

LOCAL Temp LOCAL NumToConvert LOCAL cDollars, cCents LOCAL nDecimalPlace, nCount LOCAL ARRAY aPlace[9]

DO CASE CASE EMPTY(tNumToConvert) NumToConvert = &quot;0&quot; CASE TYPE(&quot;tNumToConvert&quot;) = &quot;N&quot; && Convert to positive character string *!* Check to see if the number is too big for VFP. If the number is too large, exit IF tNumToConvert > 9999999999999.99 RETURN &quot;**,***,***,***,***.**&quot; ENDIF * Convert tnNumToConvert to a positive string, trimming extra spaces. NumToConvert = ALLTRIM(STR(ABS(tNumToConvert), 16, 2)) OTHERWISE && Parameter is character based, make sure it is not negative. * Convert the number to a positive number. NumToConvert = IIF(LEFT(ALLTRIM(tNumToConvert), 1) = &quot;-&quot;, SUBSTR(ALLTRIM(tNumToConvert), 2), ALLTRIM(tNumToConvert)) ENDCASE

nDecimalPlace = AT(&quot;.&quot;, NumToConvert)
 * Find decimal place.

STORE &quot;&quot; TO cDollars, cCents, Result

aPlace = &quot;&quot; aPlace[2] = &quot; Thousand &quot; aPlace[3] = &quot; Million &quot; aPlace[4] = &quot; Billion &quot; aPlace[5] = &quot; Trillion &quot; aPlace[6] = &quot; Quadrillion &quot; aPlace[7] = &quot; Quintillion &quot; aPlace[8] = &quot; Sextillion &quot; aPlace[9] = &quot; Septillion &quot;

IF nDecimalPlace > 0 * Convert cents Temp = LEFT(SUBSTR(NumToConvert, nDecimalPlace + 1) + &quot;00&quot;, 2) cCents = ConvertTens(Temp)
 * If you find decimal place...

* Strip off cents from remainder to convert. NumToConvert = ALLTRIM(LEFT(NumToConvert, nDecimalPlace - 1)) ENDIF

nCount = 1 DO WHILE NOT NumToConvert == &quot;&quot; * Convert last 3 digits of NumToConvert to English dollars. Temp = ConvertHundreds(RIGHT(NumToConvert, 3)) IF NOT Temp == &quot;&quot; cDollars = Temp + aPlace[nCount] + cDollars ENDIF IF LEN(NumToConvert) > 3 Then * Remove last 3 converted digits from NumToConvert. NumToConvert = LEFT(NumToConvert, LEN(NumToConvert) - 3) ELSE NumToConvert = &quot;&quot; ENDIF nCount = nCount + 1 ENDDO

DO CASE CASE cDollars == &quot;&quot; cDollars = &quot;No Dollars&quot; CASE cDollars == &quot;One&quot; cDollars = &quot;One Dollar&quot; OTHERWISE cDollars = cDollars + &quot; Dollars&quot; ENDCASE
 * Clean up dollars.

DO CASE CASE cCents == &quot;&quot; cCents = &quot; And No Cents&quot; CASE cCents == &quot;One&quot; cCents = &quot; And One Cent&quot; OTHERWISE cCents = &quot; And &quot; + cCents + &quot; Cents&quot; ENDCASE
 * Clean up cents.

ConvertCurrencyToEnglish = cDollars + cCents RETURN ConvertCurrencyToEnglish ENDFUNC

FUNCTION ConvertHundreds LPARAMETERS NumToConvert LOCAL Result Result = &quot;&quot;

* Exit if there is nothing to convert. IF VAL(NumToConvert) = 0 RETURN Result ENDIF

* Append leading zeros to number. NumToConvert = RIGHT(&quot;000&quot; + NumToConvert, 3)

* Do you have a hundreds place digit to convert?' IF LEFT(NumToConvert, 1) <> &quot;0&quot; Then Result = ConvertDigit(LEFT(NumToConvert, 1)) + &quot; Hundred &quot; ENDIF

* Do you have a tens place digit to convert?' IF SUBSTR(NumToConvert, 2, 1) <> &quot;0&quot; Then Result = Result + ConvertTens(SUBSTR(NumToConvert, 2)) ELSE * If not, then convert the ones place digit. Result = Result + ConvertDigit(SUBSTR(NumToConvert, 3)) ENDIF

ConvertHundreds = ALLTRIM(Result) RETURN ConvertHundreds ENDFUNC

FUNCTION ConvertTens LPARAMETERS MyTens LOCAL Result Result = &quot;&quot;

* Is value between 10 and 19?' IF VAL(LEFT(MyTens, 1)) = 1 DO CASE CASE VAL(MyTens) = 10 Result = &quot;Ten&quot; CASE VAL(MyTens) = 11 Result = &quot;Eleven&quot; CASE VAL(MyTens) = 12 Result = &quot;Twelve&quot; CASE VAL(MyTens) = 13 Result = &quot;Thirteen&quot; CASE VAL(MyTens) = 14 Result = &quot;Fourteen&quot; CASE VAL(MyTens) = 15 Result = &quot;Fifteen&quot; CASE VAL(MyTens) = 16 Result = &quot;Sixteen&quot; CASE VAL(MyTens) = 17 Result = &quot;Seventeen&quot; CASE VAL(MyTens) = 18 Result = &quot;Eighteen&quot; CASE VAL(MyTens) = 19 Result = &quot;Nineteen&quot; OTHERWISE ENDCASE ELSE * .. otherwise it's BETWEEN 20 AND 99. DO CASE CASE VAL(LEFT(MyTens, 1)) = 2 Result = &quot;Twenty &quot; CASE VAL(LEFT(MyTens, 1)) = 3 Result = &quot;Thirty &quot; CASE VAL(LEFT(MyTens, 1)) = 4 Result = &quot;Forty &quot; CASE VAL(LEFT(MyTens, 1)) = 5 Result = &quot;Fifty &quot; CASE VAL(LEFT(MyTens, 1)) = 6 Result = &quot;Sixty &quot; CASE VAL(LEFT(MyTens, 1)) = 7 Result = &quot;Seventy &quot; CASE VAL(LEFT(MyTens, 1)) = 8 Result = &quot;Eighty &quot; CASE VAL(LEFT(MyTens, 1)) = 9 Result = &quot;Ninety &quot; OTHERWISE ENDCASE

* Convert ones place digit. Result = Result + ConvertDigit(RIGHT(MyTens, 1)) ENDIF ConvertTens = Result RETURN ConvertTens ENDFUNC

FUNCTION ConvertDigit LPARAMETERS MyDigit DO CASE CASE VAL(MyDigit) = 1 ConvertDigit = &quot;One&quot; CASE VAL(MyDigit) = 2 ConvertDigit = &quot;Two&quot; CASE VAL(MyDigit) = 3 ConvertDigit = &quot;Three&quot; CASE VAL(MyDigit) = 4 ConvertDigit = &quot;Four&quot; CASE VAL(MyDigit) = 5 ConvertDigit = &quot;Five&quot; CASE VAL(MyDigit) = 6 ConvertDigit = &quot;Six&quot; CASE VAL(MyDigit) = 7 ConvertDigit = &quot;Seven&quot; CASE VAL(MyDigit) = 8 ConvertDigit = &quot;Eight&quot; CASE VAL(MyDigit) = 9 ConvertDigit = &quot;Nine&quot; OTHERWISE ConvertDigit = &quot;&quot; ENDCASE RETURN ConvertDigit ENDFUNC  Save the program as convertcurrencytoenglish.prg To test this program, type the following line in the Command window, and then press ENTER: ? ConvertCurrencyToEnglish(1234.56) 

NOTE: The largest number that this program accomodates is 9,999,999,999,999.99 due to product limitations.

