« Reply #47 on: December 17, 2018, 05:45:42 PM »
Here are a pair of complementary functions:
Integer to Words(defun LM:int
->words
( n
/ f1 f2
) (nth (fix n
) '
("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen")) (strcat (nth (- (fix (/ n
10)) 2) '
("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")) " " (f1
(rem n
10))) )
)
)
)
"zero"
(f2 n
'(
(1e18 "quintillion")
(1e15 "quadrillion")
(1e12 "trillion")
(1e09 "billion")
(1e06 "million")
(1e03 "thousand")
(1e02 "hundred")
)
)
)
)
)
Words to Integer(defun LM:words
->int
( s
/ f1 f2
) '("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen")
)
)
( 0 )
)
)
)
)
( (null l
) (f1 s '
("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety"))) )
)
0
'(
("quintillion" 1e18)
("quadrillion" 1e15)
("trillion" 1e12)
("billion" 1e09)
("million" 1e06)
("thousand" 1e03)
("hundred" 1e02)
)
)
)
)
Examples:_$
(rtos (LM:words
->int
(LM:int
->words
3)) 2 0)"3"
_$
(rtos (LM:words
->int
(LM:int
->words
31)) 2 0)"31"
_$
(rtos (LM:words
->int
(LM:int
->words
314)) 2 0)"314"
_$
(rtos (LM:words
->int
(LM:int
->words
3141)) 2 0)"3141"
_$
(rtos (LM:words
->int
(LM:int
->words
31415)) 2 0)"31415"
_$
(rtos (LM:words
->int
(LM:int
->words
314159)) 2 0)"314159"
_$
(rtos (LM:words
->int
(LM:int
->words
3141592)) 2 0)"3141592"
_$
(rtos (LM:words
->int
(LM:int
->words
31415926)) 2 0)"31415926"
_$
(rtos (LM:words
->int
(LM:int
->words
314159265)) 2 0)"314159265"
_$
(rtos (LM:words
->int
(LM:int
->words
3141592653)) 2 0)"3141592653"
_$
(rtos (LM:words
->int
(LM:int
->words
31415926535)) 2 0)"31415926535"
_$
(rtos (LM:words
->int
(LM:int
->words
314159265358)) 2 0)"314159265358"
_$ (LM:int->words 314159265358)
"three hundred fourteen billion one hundred fifty nine million two hundred sixty five thousand three hundred fifty eight"
_$
(rtos (LM:words
->int
"three hundred fourteen billion one hundred fifty nine million two hundred sixty five thousand three hundred fifty eight") 2 0)"314159265358"
« Last Edit: December 17, 2018, 06:03:54 PM by Lee Mac »
Logged