Pages

Wednesday, August 18, 2010

Money in Word function with Powerbuilder

Usually, we must write down the value of money in Invoice Document, to prevent user to cheat the value. Let say, US$ 99,80 will write as Ninety Nine Dollar and Eighty Cent.

This is the function script to write the money value in string.

First, we create f_translate function below. The function has a variable to pass called iNilai with Integer as data type. iNilai will represent the value of number that will return in word. So, the function will return a variable in string. This function will called at the main function.

string aSatuan[19], str, aPuluhan[9]
integer nRatus, nPuluh
aSatuan = { "one", "two", "three", "four", "five", &
            "six", "seven", "eight", "nine", "ten", &
            "eleven", "twelve", "thirteen", "forteen", &
            "fifteen", "sixteen", "seventeen", "eighteen", &
            "nineteen" }
aPuluhan = { "ten", "twenty", "thirty", "forty", "fifty", &
             "sixty", "seventy", "eighty", "ninety"}
               
IF nilai >= 100 THEN
   nRatus = Integer(String(nilai/100))
   IF nRatus = 1 THEN
      str += "one hundred"
   ELSE
      str += aSatuan[nRatus] + " hundreds"
   END IF
   IF nilai > 0 THEN nilai -= nRatus * 100
END IF

IF nilai >= 20 THEN
   nPuluh = Integer(String(nilai/10))
   IF nRatus > 0 THEN
      str += " and " + aPuluhan[nPuluh]
   ELSE
      str += aPuluhan[nPuluh]
   END IF
   nilai -= nPuluh * 10
   IF nilai > 0 THEN str += " " + aSatuan[nilai]
ELSE
   IF nilai > 0 THEN
      IF nRatus > 0 THEN
         str += " and " + aSatuan[nilai]
      ELSE
         str += aSatuan[nilai]
      END IF
   END IF
END IF   
                 
RETURN str


The main function is called f_moneystr with amount as a decimal variable to pass.

string str
decimal nMilyar, nJuta, nRibu

IF amount < 0 THEN RETURN ""
IF amount < 1 THEN str = "zero"

IF amount >= 1000000000000.00 THEN
   nMilyar = Integer(String(amount/1000000000000.00))
   str += f_Translate(nMilyar) + " trillion "
   amount -= (nMilyar * 1000000000000.00)
END IF

IF amount >= 1000000000 THEN
   nMilyar = Integer(String(amount/1000000000))
   IF nMilyar = 1 THEN
      str += "one billion "
   ELSE
      str += f_Translate(nMilyar) + " billion "
   END IF
   amount -= (nMilyar * 1000000000)
END IF

IF amount >= 1000000 THEN
   nJuta = Integer(String(amount/1000000))
   IF nJuta = 1 THEN
      str += "one million "
   ELSE
      str += f_Translate(nJuta) + " millions "
   END IF
   amount -= (nJuta * 1000000)
END IF

IF amount >= 1000 THEN
   nRibu = Integer(String(amount/1000))
   IF nRibu = 1 THEN
      str += "one thousand "
   ELSE
      str += f_Translate(nRibu) + " thousands "
   END IF
   amount -= (nRibu * 1000)
END IF

str += f_Translate(Integer(String(amount)))
str = RightTrim(str) + " US Dollar"
amount -= Integer(String(amount))
IF amount > 0 THEN
   amount *= 100
   str += " and " + f_Translate(amount) + " cent"
END IF
   
return str

To use this function, just call with this statement:

string sMoney
sMoney = f_moneystr(99.80)

Note: Thanks to Mr. AGP for the script :)

No comments:

Post a Comment