Convert Numbers into Words

Share this topic:



Link to this posting

Postby Cabiles Raymond » 22 Jul 2014, 09:00

something wrong with my decimal. it wont display in words.. it displays in numbers. ex: 772.20 it display " SEVEN HUNDREDSEVENTY TWO PESOS AND 20 CENTS" not " SEVEN HUNDREDSEVENTY TWO PESOS AND TWENTY CENTS


String ls_Single[], ls_Ten[], ls_Teen[], ls_Mega[]
String ls_Hundred, ls_Word, ls_Char, ls_Temp,ls_Cents, ls_Amount
Integer li_Point
Long ll_Position
ls_Single = { ' ONE',' TWO',' THREE',' FOUR',' FIVE','SIX',' SEVEN','EIGHT',' NINE' }
ls_Ten = { ' TEN',' TWENTY',' THIRTY',' FORTY',' FIFTY','SIXTY','SEVENTY',' EIGHTY',' NINETY' }
ls_Teen = { ' TEN', ' ELEVEN',' TWELVE',' THIRTEEN','FOURTEEN','FIFTEEN',' SIXTEEN',' SEVENTEEN',' EIGHTEEN',' NINETEEN' }
ls_Mega = { '','THOUSAND','MILLION','BILLION'}
ls_Hundred = 'HUNDRED'
li_Point = 1

ls_Amount = Trim(String(sle_1.text))
ll_Position = Pos(ls_Amount,'.')
If ll_Position > 0 THEN
ls_Cents = Mid(ls_Amount,ll_Position + 1,2)
ls_Amount = Mid(ls_Amount,1,ll_Position - 1)


END IF

Do while Len(ls_Amount) > 0
ls_Word = ''
ls_Temp = Right(ls_Amount,3)
Do While Len(ls_Temp) > 0
CHOOSE CASE Len(ls_Temp)
CASE 3
ls_Char = Mid(ls_Temp,1,1)
If ls_Char <> '0' THEN
ls_Word += ls_Single[Integer(ls_Char)] + ' ' +ls_Hundred
END IF
ls_Temp = Mid(ls_Temp,2)
CASE 2
ls_Char = Mid(ls_Temp,1,1)
If ls_Char = '0' THEN
ls_Temp = Mid(ls_Temp,2,1)
Else
If ls_Char = '1' THEN
ls_Word += ls_Teen[Integer(Mid(ls_Temp,2,1))+1]
ls_Temp = ''
Else
ls_Word += ls_Ten[Integer(ls_Char)]
ls_Temp = Mid(ls_Temp,2,1)
END IF
END IF
CASE 1
ls_Char = Mid(ls_Temp,1,1)
If ls_Char <> '0' THEN
ls_Word += ls_Single[Integer(ls_Char)]
END IF
ls_Temp = ''
END CHOOSE
LOOP
//
ls_Amount = Mid(ls_Amount,1,Len(ls_Amount)-3)
If Len(ls_Word) > 0 THEN
mle_1.text = RightTrim(ls_Word) + ' ' + ls_Mega[li_Point] +RightTrim(mle_1.text)
END IF
li_Point++
LOOP
// //
mle_1.text = RightTrim(mle_1.text)
IF Integer(ls_Amount) = 1 THEN

mle_1.text += ' PESOS and '

END IF
mle_1.text += ' PESOS AND '
mle_1.text += ls_Cents + ' CENTS'
Return
User avatar
Cabiles Raymond
 
Posts: 1
Joined: 19 Jul 2014, 03:50

Link to this posting

Postby seki » 15 Oct 2014, 02:39

Sure, at the line before last one, you are concatenating ls_Cents that is just the part of the amount after the decimal point (if any), and not a processed string. ;)

Some remarks about your code :
  • You should extract the processing loop into a function and call it twice on both parts of the amount (the integer part and the decimal part).
  • it is poor design to work directly on sle and mle texts, you should abstract it as arguments of a function for a better decoupling and easier reuse in another context.
  • your management of spaces to separate words look a little inconsistent : not all words are defined with spaces and the usage of trims look like some attempt to clean text afterwards. It is better to but spaces only when needed.

:ugeek: I have made a userobject based on you code that implements my remarks and displays consistent results for what I have tested:
Code: Select all
forward
global type counter from nonvisualobject
end type
end forward

global type counter from nonvisualobject autoinstantiate
end type

forward prototypes
public function string count (string as_val)
public function string stringify (string as_val)
end prototypes

public function string count (string as_val);
String ls_Amount, ls_cents
string ls_ret
Long ll_Position, ll_amount, ll_cents

ls_Amount = Trim(as_val)
ll_Position = Pos(ls_Amount,'.')
If ll_Position > 0 THEN
   ls_Cents = Mid(ls_Amount,ll_Position + 1,2)
   ll_cents = Long(ls_cents)
   ls_Amount = Mid(ls_Amount,1,ll_Position - 1)
   ll_amount = long(ls_amount)
END IF

ls_ret = stringify(ls_amount)

IF ll_amount > 1 THEN
   ls_ret += ' PESOS'
else
   ls_ret += ' PESO'
END IF
if ll_cents > 0 then
   ls_ret += ' AND ' + stringify(ls_cents)
   if ll_cents > 1 then
      ls_ret += ' CENTS'
   else
      ls_ret += ' CENT'
   end if
end if


Return ls_ret

end function

public function string stringify (string as_val);
String ls_Single[], ls_Ten[], ls_Teen[], ls_Mega[]
String ls_Hundred, ls_Word, ls_Char, ls_Temp, ls_amount, ls_unit
string ls_ret
Integer li_Point
Long ll_Position
ls_Single = { 'ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE' }
ls_Ten = { 'TEN','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY' }
ls_Teen = { 'TEN', 'ELEVEN','TWELVE','THIRTEEN','FOURTEEN','FIFTEEN','SIXTEEN','SEVENTEEN','EIGHTEEN','NINETEEN' }
ls_Mega = { '','THOUSAND','MILLION','BILLION'}
ls_Hundred = 'HUNDRED'
li_Point = 1

ls_amount = as_val
Do while Len(ls_Amount) > 0
   ls_Word = ''
   ls_Temp = Right(ls_Amount,3)
   Do While Len(ls_Temp) > 0
      if len(ls_word) > 0 then ls_word += ' '
      CHOOSE CASE Len(ls_Temp)
         CASE 3
            ls_Char = Mid(ls_Temp,1,1)
            If ls_Char <> '0' THEN
               ls_Word += ls_Single[Integer(ls_Char)] + ' ' +ls_Hundred
            END IF
            ls_Temp = Mid(ls_Temp,2)
         CASE 2
            ls_Char = Mid(ls_Temp,1,1)
            If ls_Char = '0' THEN
               ls_Temp = Mid(ls_Temp,2,1)
            Else
               If ls_Char = '1' THEN
                  ls_Word += ls_Teen[Integer(Mid(ls_Temp,2,1))+1]
                  ls_Temp = ''
               Else
                  ls_Word += ls_Ten[Integer(ls_Char)]
                  ls_Temp = Mid(ls_Temp,2,1)
               END IF
            END IF
         CASE 1
            ls_Char = Mid(ls_Temp,1,1)
            If ls_Char <> '0' THEN
               ls_Word += ls_Single[Integer(ls_Char)]
            END IF
            ls_Temp = ''
      END CHOOSE
   LOOP

   ls_Amount = Mid(ls_Amount,1,Len(ls_Amount)-3)
   If Len(ls_Word) > 0 THEN
      if len(ls_ret) > 0 then ls_ret = ' ' + ls_ret
      ls_unit = ls_Mega[li_Point]
      if len(ls_unit) > 0 then
         ls_ret = ls_Word + ' ' + ls_unit + ls_ret
      else
         ls_ret = ls_Word + ls_ret
      end if
   END IF
   li_Point++
LOOP

Return ls_ret

end function

on counter.create
call super::create
TriggerEvent( this, "constructor" )
end on

on counter.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

seki
 
Posts: 3
Joined: 02 Apr 2013, 07:47

Link to this posting

Postby Ursego » 20 Oct 2014, 13:00

Seki, I only copy-pasted a code, found in the Internet - as an example of the algorithm. Of course, everybody should accomodate it to their needs. Thanks for the NVO!
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33


Return to Tips and Tricks

Who is online

Users browsing this forum: Bing [Bot] and 1 guest


Convert Numbers into Words

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





cron
free counters

eXTReMe Tracker