Convert Numbers into Words


Link to this posting

Postby Cabiles Raymond » 22 Jul 2014, 09:00

1. Create an empty NonVisualObject named n_num_to_words.
2. Open it in the "Edit Source" mode and replace the whole text with the following code:

Code: Select all
forward
global type n_num_to_words from nonvisualobject
end type
end forward

global type n_num_to_words from nonvisualobject autoinstantiate
end type

forward prototypes
public function string uf_count (string as_val)
private function string uf_stringify (string as_val)
end prototypes

public function string uf_count (string as_val);string   ls_amount
string   ls_cents
string   ls_ret
long      ll_pos
long      ll_amount
long      ll_cents

ls_amount = Trim(as_val)
ll_pos = Pos(ls_amount, '.')
if ll_pos > 0 then
   ls_cents = Mid(ls_amount, ll_pos + 1, 2)
   ll_cents = Long(ls_cents)
   ls_amount = Mid(ls_amount, 1, ll_pos - 1)
   ll_amount = Long(ls_amount)
end if

ls_ret = uf_stringify(ls_amount)
ls_ret += ' DOLLAR'

if ll_amount > 1 then ls_ret += 'S'

if ll_cents > 0 then
   ls_ret += ' AND ' + uf_stringify(ls_cents)
   ls_ret += ' CENTS'
   if ll_cents > 1 then ls_ret += 'S'
end if

return ls_ret

end function

private function string uf_stringify (string as_val);string   ls_single[] = { 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE' }
string   ls_ten[] = { 'TEN', 'TWENTY', 'THIRTY', 'FORTY', 'FIFTY', 'SIXTY', 'SEVENTY', 'EIGHTY', 'NINETY' }
string   ls_teen[] = { 'TEN', 'ELEVEN', 'TWELVE', 'THIRTEEN',' FOURTEEN', 'FIFTEEN', 'SIXTEEN', 'SEVENTEEN', 'EIGHTEEN', 'NINETEEN' }
string   ls_mega[] = { '', 'THOUSAND', 'MILLION', 'BILLION'}
string   ls_hundred = 'HUNDRED'
string   ls_word
string   ls_char
string   ls_temp
string   ls_amount
string   ls_unit
string   ls_ret
int      li_point = 1
long      ll_pos

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 n_num_to_words.create
call super::create
TriggerEvent( this, "constructor" )
end on

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



2. Save.
User avatar
Cabiles Raymond
 
Posts: 1
Joined: 19 Jul 2014, 03:50



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker