2 @program english-numbers.muf
5 $note A simple routine to render a numeric value as english words.
12 doesn't check for overflow
13 doesn't do fancy shortening, like someteen-hundred
18 $author <justin.wind@gmail.com>
21 ( trim some debug fluff )
22 $def D_SAVE prog "D" flag? D_STATE !
23 $def D_UNSET prog "!D" set
24 $def D_RESTORE D_STATE @ if prog "D" set then
35 ( the numbers, up until things become predictable )
36 : low_numbers_ ( @ -- s )
38 { "" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten"
39 "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen"
48 { "" "" "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety"
53 ( just to keep things stylistically similar )
57 ( words for big numbers, according to wikipedia )
58 : big_numbers_ ( @ -- s )
60 { "" "thousand" "million" "billion" "trillion" "quadrillion"
61 "quintillion" "sextillion" "septillion" "octillion" "nonillion"
62 "decillion" "undecillion" "duodecillion" "tredecillion"
63 "quattuordecillion" "quindecillion" "sexdecillion" "septendecillion"
64 "octodecillion" "novemdecillion" "vigintillion" "silmarillion"
70 ( outputs the words for a number under one-thousand )
71 : english_sub_thousand_ ( i -- s )
73 dup 1000 % 100 / var! HUNDREDS
74 dup 100 % 10 / var! TENS
77 ( show hundreds if they exist )
80 HUNDREDS @ low_numbers_ strcat
83 ( if there's more number to show, need a word break )
90 ( show remaining teens or under )
93 over 20 % low_numbers_ strcat
96 ( otherwise show remaining tens-ones pattern )
98 TENS @ decades_ strcat
101 ONES @ low_numbers_ strcat
110 : english-number ( i -- s )
116 dup sign 0 < var! ISNEG
127 dup 1000 % english_sub_thousand_ ( i s )
128 BIGNESS @ dup 1 >= if ( i s i )
129 big_numbers_ " " swap strcat ( i s s )
135 dup strlen if ( i s s )
141 1000 / dup 0 = ( i i )
146 "negative " swap strcat
149 public english-number
157 @reg english-numbers.muf=lib/squeep/english-numbers
158 @set $lib/squeep/english-numbers=_docs:@list english-numbers.muf=1-10
159 @set $lib/squeep/english-numbers=L
160 @set $lib/squeep/english-numbers=V
162 @set #0=_msgmacs/english-number:{muf:$lib/squeep/english-numbers,{:1}}