From 8f65b89a9c48bbe026e9a2cd5c17ded31f0f460e Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Sat, 2 Feb 2013 11:35:49 -0800 Subject: [PATCH] The string-parsing version of the function now works for arbitrarily-large numbers. Implemention suggested by Stacey. --- english-numbers.muf | 59 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/english-numbers.muf b/english-numbers.muf index dc2c748..ad3f69e 100644 --- a/english-numbers.muf +++ b/english-numbers.muf @@ -6,17 +6,19 @@ $note A simple routine to render a numeric value as english words. $libdef english-number ( english-numbers.muf Dec 11 2012 - v1.000 + Feb 2 2012 - v1.001 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 -$version 1.000 +$version 1.001 $author +$undef DEBUGGING $ifdef DEBUGGING ( trim some debug fluff ) $def D_SAVE prog "D" flag? D_STATE ! @@ -64,6 +66,7 @@ $endif "octodecillion" "novemdecillion" "vigintillion" "silmarillion" } array_make swap array_getitem + dup 0 = if pop "???-illion" then D_RESTORE ; @@ -148,8 +151,56 @@ $endif ; public english-number -: 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 ; . c -- 2.45.2