Thursday 24 November 2016

How To Convert Number To Words In Visual Foxpro 9.0

I created these funtions a long time ago, but I believe that there are still a number of software developers out there who are using VFP as their primary or secondary programming language.

To use this, save all these codes in a singe PRG file. To call the function anywhere in your VFP program, use this syntax (please do not use commas):

? NumToWord(12437.95)

****************************************
* Demo on converting number to words
* Primary usage: Philippine Local Checks
* Author: Glen T. Villar
* Date Created: January 13, 2009
* Code Updated: 12 April 2019
*****************************************

Function NumToWord(Par1 As Long)
 If Mod(Par1,1) > 0
  m.lcNewItem = FixItUp(Par1)+' Dollar and '+FixItUp(VAL(RIGHT(TRANSFORM(Par1,"9999999999999999999.99"),2)))+' Cents'
 Else
  m.lcNewItem = FixItUp(Par1)+' Dollar'
 Endif
 Return (m.lcNewItem)
Endfunc

Function FixItUp(Param1 As Long)
 ****************************************
 *  Author: Glen T. Villar
 *  Function for Converting Number to Words
 *  Primary usage: Philippine Local Checks
 *  Secondary usage: American Local Checks
 *  Date Created: January 13, 2009
 *  Date Modified: August 8, 2009
 *  Date Modified: April 12, 2019  
 *****************************************
 If !Empty(Param1)
  Dimension lArray[6]
  Local lcAnswer, nReiterate, lnLeftSide, lcWord, lcConcatenate, ;
   lnMove, lcTaken, lcWordsExt
  If !Empty(Param1)
   lcLeftPart = Alltrim(Transform(Int(Param1)))
   lnLeftSide = Len(m.lcLeftPart)
   nReiterate = 0
   For lnVar = 1 To Int(m.lnLeftSide/3)
    lnLeftSide = lnLeftSide - 3
    nReiterate = nReiterate + 1
   Endfor

   lcOnes = '1One,2Two,3Three,4Four,5Five,6Six,7Seven,8Eight,9Nine,'
   lcTees = '10Ten,11Eleven,12Twelve,13Thirteen,14Fourteen,15Fifteen,16Sixteen,17Seventeen,18Eighteen,19Nineteen,'
   lcTens = '2Twenty,3Thirty,4Forty,5Fifty,6Sixty,7Seventy,8Eighty,9Ninety,'
   lcTitle = '2Thousand,3Million,4Billion,5Thrillion,6Quadrillion,'

   lnMove = m.nReiterate * 3
   lcParam = Alltrim(Right(m.lcLeftPart,m.lnMove))
   lcClassify = ''
   lcClassify = Iif(Between(Mod(Val(Alltrim(Left(m.lcLeftPart,m.lnLeftSide)))/100,1) * 100,1,9), Strextract(m.lcOnes,Alltrim(Left(m.lcLeftPart,m.lnLeftSide)),','), ;
    IIF(Between(Mod(Val(Alltrim(Left(m.lcLeftPart,m.lnLeftSide)))/100,1) * 100,10,19), Strextract(m.lcTees,Alltrim(Left(m.lcLeftPart,m.lnLeftSide)),','), ;
    IIF(Between(Mod(Val(Alltrim(Left(m.lcLeftPart,m.lnLeftSide)))/100,1) * 100,20,99), Strextract(m.lcTens,Alltrim(Left(m.lcLeftPart,1)),',')+Space(1)+ ;
    Strextract(m.lcOnes,Alltrim(Substr(m.lcLeftPart,2,1)),','),'')))

   For lnVar = 1 To m.nReiterate
    lnMove = m.lnMove-3
    lcTaken = Substr(lcParam,m.lnMove+1,3)

    If Mod((Val(m.lcTaken)/100),1) * 100 > 19
     lcConcatenate = Strextract(m.lcOnes,Alltrim(Left(m.lcTaken,1)),',')+Iif(Alltrim(Left(m.lcTaken,1))<>'0',' Hundred ','')+ ;
      Strextract(m.lcTens,Alltrim(Substr(m.lcTaken,2,1)),',')+Space(1)+Strextract(m.lcOnes,Alltrim(Right(m.lcTaken,1)),',')
    Else
     lcConcatenate = Strextract(m.lcOnes,Alltrim(Left(m.lcTaken,1)),',')+ Iif(Alltrim(Left(m.lcTaken,1))<>'0',' Hundred ','')+;
      Strextract(m.lcTees,Alltrim(Substr(m.lcTaken,2,2)),',')+Iif(Mod(Val(m.lcTaken)/100,1) * 100 < 11,Strextract(m.lcOnes,Alltrim(Right(m.lcTaken,1)),','),;
      Strextract(m.lcTens,Alltrim(Substr(m.lcTaken,2,2)),','))
    Endif
    lArray[lnVar] = m.lcConcatenate + Space(1) + Strextract(m.lcTitle,Alltrim(Str(m.lnVar)),',')
   Endfor

   lcWordsExt = ''
   For nVal = m.nReiterate To 1 Step -1
    lcWordsExt = lcWordsExt + Space(1) + ;
     Iif(Getwordcount(lArray[nVal])=1 And Inlist(Upper(Alltrim(lArray[nVal])),'THOUSAND','MILLION'),'',lArray[nVal])
   Endfor

   lcNewWord = ''
   lcNewWord = m.lcClassify + Space(1)+;
    IIF(!Empty(m.lcClassify),Alltrim(Right(Getwordnum(m.lcTitle,m.nReiterate,','),Len(Getwordnum(m.lcTitle,m.nReiterate,','))-1)),'');
    +Space(1)+;
    Alltrim(m.lcWordsExt)

   Return Alltrim(m.lcNewWord)
  Endif
 Else
  Return ''
 Endif
Endfunc

6 comments:

  1. Good useful information about converting number to words.

    Visual FoxPro to .Net

    ReplyDelete
  2. Thanks for this useful function but I think the conversion is not accurate

    Try these

    ? NumToWord(2367493316.10)

    ? Num2Word(2316161342.93)

    ReplyDelete
    Replies
    1. Hi Unknown,

      Thanks for dropping by.

      I created this conversion routine in 2009 when I was on a holiday with my family on a beach somewhere in Bolinao, Pangasinan, Philippines. I kind of got bored back then and thought I should write something useful that I could share for free to anyone, hence, I came up with this.

      Anyway, thank you for pointing that out. The problem was in the decimal side. I fixed it and updated the codes on this post.

      Enjoy using this function.

      Regards,

      Glen

      Delete
  3. hi there, i want to convert it in my lanague urdu, in my lanague all numbers from zero to hundred have unique name, not like english where after twenty you repeat one,two,three and after thirty and fourty. plz help me

    ReplyDelete
    Replies
    1. Hi Arshad,

      Thanks for dropping by. I'm not familiar with the Urdu Language. Currently, this program is for English currency only.

      Delete
  4. HOW TO INSERT YOUR PRG file in my FORM created using the wizard

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...