Saturday, January 31, 2009

Convert Number/Currency to Words

This article is from http://support.microsoft.com/kb/259663

To create the ConvertCurrencyToEnglish() function, follow these steps:

  1. Create a new program and save it as ConvertCurrencyToEnglish:
  2. Copy and paste the following code to the new program:


  3. *FUNCTION ConvertCurrencyToEnglish
    LPARAMETERS tNumToConvert
    LOCAL Temp
    LOCAL NumToConvert
    LOCAL cPesos, cCentavos
    LOCAL nDecimalPlace, nCount
    LOCAL ARRAY aPlace[9]

    DO CASE
    CASE EMPTY(tNumToConvert)
    NumToConvert = "0"
    CASE TYPE("tNumToConvert") = "N" && 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 "**,***,***,***,***.**"
    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) = "-", SUBSTR(ALLTRIM(tNumToConvert), 2), ALLTRIM(tNumToConvert))
    ENDCASE

    * Find decimal place.
    nDecimalPlace = AT(".", NumToConvert)

    STORE "" TO cPesos, cCentavos, Result

    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 "


    * If you find decimal place...
    IF nDecimalPlace > 0
    * Convert centavos
    Temp = LEFT(SUBSTR(NumToConvert, nDecimalPlace + 1) + "00", 2)
    cCentavos = ConvertTens(Temp)

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

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

    * Clean up pesos.
    DO CASE
    CASE cPesos == ""
    cPesos = "No Pesos"
    CASE cPesos == "One"
    cPesos = "One Peso"
    OTHERWISE
    cPesos = cPesos + " Pesos"
    ENDCASE

    * Clean up centavos.
    DO CASE
    CASE cCentavos == ""
    cCentavos = " And No Centavos"
    CASE cCentavos == "One"
    cCentavos = " And One Centavo"
    OTHERWISE
    cCentavos = " And " + cCentavos + " Centavos"
    ENDCASE

    ConvertCurrencyToEnglish = cPesos + cCentavos
    RETURN ConvertCurrencyToEnglish
    ENDFUNC

    FUNCTION ConvertHundreds
    LPARAMETERS NumToConvert
    LOCAL Result
    Result = ""

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

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

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

    * Do you have a tens place digit to convert?'
    IF SUBSTR(NumToConvert, 2, 1) <> "0" 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 = ""

    * Is value between 10 and 19?'
    IF VAL(LEFT(MyTens, 1)) = 1
    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"
    OTHERWISE
    ENDCASE
    ELSE
    * .. otherwise it's BETWEEN 20 AND 99.
    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 "
    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 = "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


  4. Save the program as convertcurrencytoenglish.prg
  5. 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.

    Monday, January 26, 2009

    Having them back


    Having a what they called "friends" in a long time is a treasure, but letting them go and giving up for the friendship, is a LOST. Now i realized that no matter how far, no matter how busy person you are, no matter what, you have to take care of it, because you can never have them back when its too late.

    Sunday, January 18, 2009

    Forgot Admin PW

    1. Log-on using Administrator as user with blank pw. This works 90% of the time. But some people put pw's on the administrator account, so you have to use other means.

    2. Reset the administrator pw to blank. This is done by using a bootable media (floppy, CD, or flash disk). Examples:

    a) EBCD (Emergency Boot CD). 18MB available at http://www.simtel.net/product.download.mirrors.php?id=80024. Read more details at http://ebcd.pcministry.com/

    b) There's a small Linux boot disk small enough to fit in a floppy and which resets the administrator pw to blank. Still looking for the site.

    c) Offline NT Password & Registry Editor (v080526 - May 2008) written by Petter Nordahl-Hagen, available at http://home.eunet.no/pnordahl/ntpasswd/bd080526.zip (less than 1.4 MB for floppy boot disk), and at http://home.eunet.no/pnordahl/ntpasswd/cd080526.zip (around 3MB for mini-CD boot disk).