RATE THIS ANSWER
0
Click to Vote:
0
0
This is easy enough to write. Here's the pseudocode:
Top level:
...If the number is zero
......Return "zero"
...Call level 2 with trunc(number/1000000000000), "trillion"
...Call level 2 with mod(trunc(number/1000000000),1000), "billion"
...Call level 2 with mod(trunc(number/1000000),1000), "million"
...Call level 2 with mod(trunc(number/1000),1000), "thousand"
...Call level 2 with mod(number,1000), ""
Level 2:
...localNumber = number
...If localNumber>=100
......Call level 2 with trunc(localNumber/100), "hundred"
......localNumber = mod(localNumber,100)
...If localNumber>19
......Output from an array of "twenty", "thirty", ..., "ninety" indexed by trunc(localNumber/10)-1
......localNumber = mod(localNumber,10)
...If localNumber>0
......Output from an array of "one", "two", ..., "nineteen" indexed by localNumber
...If number>0
......Output the word passed as the second argument
If you need dollars and cents, Add this over-level:
...dollars = trunc(number)
...cents = mod(number, 1) * 100
...if dollars>0
......Output the english of dollars
......Output "dollars"
......If cents>0
.........Output "and"
.........Output the english of cents
.........Output "cents"
...else
......if cents>0
.........Output the english of cents
.........Output "cents"
......else
.........Output "zero"
Note that in other languages, the rules are different. For instance, in Spanish, >19 becomes >15, and you need to add "ten" to the first list. In Korean, you need a line in the first level for each tens place, using "chip", "ma", and all of those words.