Implemention suggested by Stacey.
$libdef english-number
( english-numbers.muf
Dec 11 2012 - v1.000
$libdef english-number
( english-numbers.muf
Dec 11 2012 - v1.000
caveats:
doesn't handle floats
caveats:
doesn't handle floats
- doesn't check for overflow
+ integer variant doesn't check for integer overflow
doesn't do fancy shortening, like someteen-hundred
)
$lib-version 1.000
doesn't do fancy shortening, like someteen-hundred
)
$lib-version 1.000
$author <justin.wind@gmail.com>
$author <justin.wind@gmail.com>
$ifdef DEBUGGING
( trim some debug fluff )
$def D_SAVE prog "D" flag? D_STATE !
$ifdef DEBUGGING
( trim some debug fluff )
$def D_SAVE prog "D" flag? D_STATE !
"octodecillion" "novemdecillion" "vigintillion" "silmarillion"
} array_make
swap array_getitem
"octodecillion" "novemdecillion" "vigintillion" "silmarillion"
} array_make
swap array_getitem
+ dup 0 = if pop "???-illion" then
-: mpi
- atoi english-number
+: strcut-end ( s i -- s s )
+ over strlen swap -
+ dup 0 <= if
+ ( requested more than string has )
+ pop "" swap
+ else
+ strcut
+ then
+;
+
+( deal with number as string )
+( assumes base10 )
+: mpi-english-number ( s -- s )
+ ( atoi english-number exit )
+ 0 var! ISNEG
+ "" var! OUT
+ 0 var! BIGNESS
+ begin ( s )
+ dup strlen while ( s )
+ 3 strcut-end ( s s )
+ atoi ( s i )
+ dup sign 0 < if
+ 1 ISNEG !
+ abs
+ then ( s i )
+ english_sub_thousand_ ( s s )
+ BIGNESS @ dup 1 >= if ( s s i )
+ big_numbers_ ( s s s )
+ " " swap strcat ( s s s )
+ strcat ( s s )
+ else
+ pop ( s s )
+ then
+ OUT @ ( s s s )
+ dup strlen if ( s s s )
+ " " swap strcat
+ then ( s s s )
+ strcat ( s s )
+ OUT ! ( s )
+
+ BIGNESS ++ ( s )
+ repeat ( s )
+ pop
+ OUT @
+ dup strlen not if
+ pop zero_
+ then
+ ISNEG @ if
+ "negative " swap strcat
+ then