As posted at The Swamp, here are a pair of complementary functions:
Integer to Words
(defun LM:int->words ( n / f1 f2 )
(defun f1 ( n )
(if (< n 20)
(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)))
)
)
(defun f2 ( n l )
(cond
( (null l) (f1 n))
( (< n (caar l)) (f2 n (cdr l)))
( (vl-string-right-trim " " (strcat (f2 (fix (/ n (caar l))) (cdr l)) " " (cadar l) " " (f2 (rem n (caar l)) (cdr l)))))
)
)
(if (zerop n)
"zero"
(vl-string-right-trim " "
(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 )
(defun f1 ( s l / p )
(cond
( (null l)
(cond
( (vl-position (vl-string-trim " " s)
'("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen")
)
)
( 0 )
)
)
( (not (setq p (vl-string-search (car l) s))) (f1 s (cdr l)))
( (+ (* 10 (- 10 (length l))) (f1 (substr s (+ 2 p (strlen (car l)))) (cdr l))))
)
)
(defun f2 ( s l / p )
(cond
( (null l) (f1 s '("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")))
( (not (setq p (vl-string-search (caar l) s))) (f2 s (cdr l)))
( (+ (* (f2 (substr s 1 p) (cdr l)) (cadar l)) (f2 (substr s (+ 2 p (strlen (caar l)))) (cdr l))))
)
)
(if (= "zero" (strcase s t))
0
(f2 (strcase s t)
'(
("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"