Number to Text Routine?
Doug Easterbrook
doug at artsman.com
Fri Jul 22 23:30:04 UTC 2022
Most of the code suggested so far seem to be somewhat based on doing it the same way. I know I got it from somebody a long time ago.
I extended it for printing checks it to handle the character conversion of numbers into the trillions - or basically the largest number that omnis handled at the time.
hope it is useful. it extends the other suggestions.
Parameters to the routine are:
pNumber field reference -> reference to any type of number (2DP, int 32, int 64, etc)
pString. field reference -> where to return the answer to
pIgnoreCentsIfNone boolean (default false) to indicate if cents should be dispalyed. sometimes on checks, if its an even dollar amount, the cents are not displayed
Local vars:
cents integer
DigitHundreds integer
DigitOnes integer
dollars integer
NextSeparator char 1
segment integer. represents where we are in groups of 3 numbers (thousands, millions, billions, trillions)
stringHundreds character 100
stringOnes character 100
stringTens. character 100
Calculate Dollars as int(pNumber)
Calculate Cents as (pNumber-Dollars)*100
# Figure out the cents portion of the string
Calculate pString as 'Dollars'
Calculate DigitOnes as mod(Cents,10)
Calculate DigitTens as (mod(Cents,100)-DigitOnes)/10
If DigitTens=0&DigitOnes=0&pIgnoreCentsIfNone
# allow ticket faces to print dollars and cents for gift certificates
Else
If DigitTens>1
Calculate StringTens as pick(DigitTens,'','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety')
Calculate StringOnes as pick(DigitOnes,'','-One','-Two','-Three','-Four','-Five','-Six','-Seven','-Eight','-Nine')
Else If DigitTens=1
Calculate StringTens as ''
Calculate StringOnes as pick(DigitOnes,'Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen')
Else
Calculate StringTens as ''
Calculate StringOnes as pick(DigitOnes,'Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine')
End If
Calculate pString as con(pString,' and ',StringTens,StringOnes,' Cents')
End If
# Do the Dollars Conversion
If Dollars=0
Calculate pString as con('Zero ',pString)
Quit method
End If
Calculate Segment as 0
Calculate NextSeparator as '' ## empty
While Dollars>0
Calculate DigitOnes as mod(Dollars,10)
Calculate DigitTens as (mod(Dollars,100)-DigitOnes)/10
Calculate DigitHundreds as (mod(Dollars,1000)-DigitTens*10-DigitOnes)/100
Calculate StringHundreds as pick(DigitHundreds,'','One Hundred','Two Hundred','Three Hundred','Four Hundred','Five Hundred','Six Hundred','Seven Hundred','Eight Hundred','Nine Hundred')
If DigitTens>1
Calculate StringTens as con(pick(DigitHundreds=0,' '),pick(DigitTens,'','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'))
Calculate StringOnes as pick(DigitOnes,'','-One','-Two','-Three','-Four','-Five','-Six','-Seven','-Eight','-Nine')
Else If DigitTens=1
Calculate StringTens as pick(DigitHundreds=0,' and ')
Calculate StringOnes as pick(DigitOnes,'Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen')
Else
Calculate StringTens as pick(DigitHundreds>0&DigitOnes>0,'',' and ')
Calculate StringOnes as pick(DigitOnes,'','One','Two','Three','Four','Five','Six','Seven','Eight','Nine')
End If
If DigitHundreds>0|DigitTens>0|DigitOnes>0
Calculate pString as con(StringHundreds,StringTens,StringOnes,' ',pick(Segment,'','Thousand','Million','Billion','Trillion'),NextSeparator,' ',pString)
Calculate NextSeparator as ','
Else
Calculate NextSeparator as ''
End If
Calculate Segment as Segment+1
Calculate Dollars as int(Dollars/1000)
End While
Do $cinst.$replace(pString,' ',' ',pString) Returns iErrorCode
Doug Easterbrook
Arts Management Systems Ltd.
mailto:doug at artsman.com
http://www.artsman.com
Phone (403) 650-1978
> On Jul 22, 2022, at 12:50 PM, Das Goravani <goravanis at gmail.com> wrote:
>
>
> Hello All,
>
> One thing I’m working on is the printing of checks.
>
> It’s one thing to have the amount as a number, that’s normal, but to print the check I need the amount in text like:
>
> One Thousand Seven Hundred Thirty Four and 68 cents
>
> Does anyone have a routine that does this conversion ?
>
> I think I could write one, but it would be a bit long, someone might do it smarter than I could do it.
>
> Thanks
>
> Das Goravani
> _____________________________________________________________
> Manage your list subscriptions at https://lists.omnis-dev.com
> Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com
More information about the omnisdev-en
mailing list