Tuesday, May 21, 2019

Number to Word Convertion Function



*********************************************************
** How to Run..
** 1. Save the following code as Num2Word.PRG
** 2. From within your PRG or Report call it as
**    Num2Word(nYourAmount)
*********************************************************
** Modify decimal places suitably, if > 2 decimals required
*********************************************************
** Num2Word.Prg
**
LPARAMETER amt
amt = ABS(amt)

IF amt > 999999999999.99 && => 1000 billion
   =MESSAGEBOX("Amount exceeds word convertion provision. Contact system administrator", ;
      0+16, "CAUTION. Check total amount !")
ENDIF
IF amt = 0
   RETURN "Zero Only."
ENDIF

** used by Function numWord
PRIVATE pcWord1, pcWord2, pcWord3, pcWord4, pcWord5, pcWord6, pcWord7, ;
   pcWord8, pcWord9, pcWord10, pcWord11, pcWord12, pcWord13, pcWord14, ;
   pcWord15, pcWord16, pcWord17, pcWord18, pcWord19, pcWord20, pcWord30, ;
   pcWord40, pcWord50, pcWord60, pcWord70, pcWord80, pcWord90
pcWord1 = "One "
pcWord2 = "Two "
pcWord3 = "Three "
pcWord4 = "Four "
pcWord5 = "Five "
pcWord6 = "Six "
pcWord7 = "Seven "
pcWord8 = "Eight "
pcWord9 = "Nine "
pcWord10 = "Ten "
pcWord11 = "Eleven "
pcWord12 = "Twelve "
pcWord13 = "Thirteen "
pcWord14 = "Fourteen "
pcWord15 = "Fifteen "
pcWord16 = "Sixteen "
pcWord17 = "Seventeen "
pcWord18 = "Eighteen "
pcWord19 = "Ninteen "
pcWord20 = "Twenty "
pcWord30 = "Thirty "
pcWord40 = "Forty "
pcWord50 = "Fifty "
pcWord60 = "Sixty "
pcWord70 = "Seventy "
pcWord80 = "Eighty "
pcWord90 = "Ninety "
**
LOCAL lcNumPhrase, lcNumStr

m.lcNumphrase = ""
m.lcNumStr = STR(amt,17,4)

IF VAL(SUBSTR(m.lcNumStr,1,3)) > 0      && Amount in Billions
   m.lcNumphrase = m.lcNumphrase + Numword(SUBSTR(m.lcNumStr,1,3)) + ;
      " Billion "
ENDIF

IF VAL(SUBSTR(m.lcNumStr,4,3)) > 0      && Amount in millions
   m.lcNumphrase = m.lcNumphrase + Numword(SUBSTR(m.lcNumStr,4,3)) + ;
      " Million "
ENDIF

IF VAL(SUBSTR(m.lcNumStr,7,3)) > 0      && Amount in thousands
   m.lcNumphrase = m.lcNumphrase + Numword(SUBSTR(m.lcNumStr,7,3)) + ;
      " Thousand "
ENDIF

IF VAL(SUBSTR(m.lcNumStr,10,3)) > 0     && Amount below thousands
   m.lcNumphrase = m.lcNumphrase + Numword(SUBSTR(m.lcNumStr,10,3))
ENDIF

IF VAL(SUBSTR(m.lcNumStr,14,2)) > 0     && Amount in Decimals
        ** needs tingering depending on digits - Default is 2 decimals
   IF LEN(ALLTRIM(m.lcNumphrase)) > 1
      m.lcNumphrase = ALLTRIM(m.lcNumphrase) + " and "
   ELSE
      m.lcNumphrase = "Zero and "
   ENDIF
   m.lcNumphrase = m.lcNumphrase + SUBSTR(m.lcNumStr,14,2) + "/100"
ENDIF

IF LEN(ALLTRIM(m.lcNumphrase)) > 1
   m.lcNumphrase = ALLTRIM(m.lcNumphrase) + " Only."
ENDIF

RETURN m.lcNumphrase
*********************************************************
** Called by: numtoword() (function  in NUMWORD.PRG)
*********************************************************
FUNCTION numword
   LPARAMETERS tStr

   LOCAL lnStr, lcPhrase, lcStr
   lcPhrase = " "
   lnStr = VAL(tStr)

   ** Hundredth position
   IF lnStr > 99
      lcStr = LEFT(tStr,1)
      lcPhrase = pcWord&lcStr + "Hundred "
   ENDIF

   ** Balance Position
   lnStr = VAL(RIGHT(tStr,2))
   IF BETWEEN(lnStr,1,20)
      lcStr = ALLTRIM(STR(lnStr))
      lcPhrase = lcPhrase + pcWord&lcStr
   ENDIF
   IF BETWEEN(lnStr,21,99)
      IF lnStr > 20
         lcStr = SUBSTR(tStr,2,1)+"0"
         lcPhrase = lcPhrase + pcWord&lcStr
      ENDIF
      IF RIGHT(tStr,1) > '0'
         lcStr = RIGHT(tStr,1)
         lcPhrase = lcPhrase + pcWord&lcStr
      ENDIF
   ENDIF
   RETURN ALLTRIM(lcPhrase)
*********************************************************
* EOF: NUM2WORD.PRG
*********************************************************
}

No comments:

Post a Comment