(06-24-2018 06:36 PM)Gerson W. Barbosa Wrote: BTW, has anyone here also written a program to fill checks (or cheques), but in English?
I am interested in the full numbers routine. Who still fills checks these days?
This is a messy-and-dirty RPL transcription of a routine I once used to create the text of the "body amount" of a check-printing application. I didn't bother including the cents part of the routine, as it merely generated "and xx/100" (xx being the two digits after the radix mark).
Apologies in advance for this -- I haven't spent any time trying to optimize it or make it more RPL-friendly. It's ugly, but I believe this is a faithful rendition of the original algorithm.
Although not needed for checks, it does handle zero, negative numbers, and larger amounts than would normally be used on a check
(x<1E45). The original also capitalized the first letter, which I didn't bother to include in this transcription.
Code:
\<<
@ digits
{ "one" "two" "three" "four" "five" "six" "seven"
"eight" "nine" }
@ teens
{ "ten" "eleven" "twelve" "thirteen" "fourteen"
"fifteen" "sixteen" "seventeen" "eighteen"
"nineteen" }
@ tens
{ "twenty" "thirty" "forty" "fifty" "sixty" "seventy"
"eighty" "ninety" }
@ magnitude
{ "thousand" "million" "billion" "trillion" "quadrillion"
"quintillion" "sextillion" "septillion" "octillion"
"nonillion" "decillion" "undecillion" "duodecillion"
"tredecillion" }
@ subroutine to append string with preceding space if appropriate
\<<
OVER SIZE { " " SWAP + } IFT +
\>>
\-> digits teens tens magnitude addstr
\<<
@ discard fractional value if real (for RPL)
IF
DUP TYPE NOT
THEN
IP
END
@ translate to zero if appropriate, otherwise proceed
IF
DUP NOT
THEN
DROP "zero"
ELSE
@ retain sign
DUP SIGN SWAP ABS
@ pre-load stack with magnitude, initial string
0. "" ROT
@ process triples until done
WHILE
DUP
REPEAT
@ get next triple
1000 IDIV2
@ split triple into hundreds and remainder
100 IDIV2
@ convert hundreds of this triple
SWAP
IF
DUP
THEN
digits SWAP GET
" hundred" +
ELSE
DROP ""
END
@ convert remainder digits of this triple
SWAP
IF
DUP
THEN
CASE
10 OVER > THEN
digits SWAP GET addstr EVAL
END
20 OVER > THEN
teens SWAP 9. - GET addstr EVAL
END
10 IDIV2 SWAP
tens SWAP 1. - GET ROT SWAP addstr EVAL
SWAP
IF
DUP
THEN
digits SWAP GET
"-" SWAP + +
ELSE
DROP
END
END
ELSE
DROP
END
@ add magnitude if appropriate
4. ROLL
IF
OVER SIZE OVER AND
THEN
" " magnitude PICK3 GET +
ROT SWAP + SWAP
END
1. + 4. ROLLD
ROT
@ accumulate new triple into final string
IF
OVER SIZE
OVER SIZE AND
THEN
" " SWAP +
END
+
SWAP
END
@ drop temporary stack values
ROT DROP2
@ prepend "negative" if appropriate
IF
SWAP 0. <
THEN
"negative " SWAP +
END
END
\>>
\>>