### Author Topic: < Challenge > Integers into words / Ordinal Numbers into words  (Read 15381 times)

0 Members and 1 Guest are viewing this topic.

#### PKENEWELL ##### Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #45 on: December 17, 2018, 09:44:29 AM »
Hello!
And how to make words-> int?
From "twenty one thousand five hundred thirty seven" in 21537.
For example.

Already created in this topic: http://www.theswamp.org/index.php?topic=43830.msg491651#msg491651
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

#### Dilan ##### Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #46 on: December 17, 2018, 05:43:05 PM »
Hello!
And how to make words-> int?
From "twenty one thousand five hundred thirty seven" in 21537.
For example.

Already created in this topic: http://www.theswamp.org/index.php?topic=43830.msg491651#msg491651
Thank you very much for the two answers. ##### Re: < Challenge > Integers into words / Ordinal Numbers into words
« Reply #47 on: December 17, 2018, 05:45:42 PM »
Here are a pair of complementary functions:

Integer to Words

Code - Auto/Visual Lisp: [Select]
1. (defun LM:int->words ( n / f1 f2 )
2.     (defun f1 ( n )
3.         (if (< n 20)
4.             (nth (fix n) '("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
5.             (strcat (nth (- (fix (/ n 10)) 2) '("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")) " " (f1 (rem n 10)))
6.         )
7.     )
8.     (defun f2 ( n l )
9.         (cond
10.             (   (null l) (f1 n))
11.             (   (< n (caar l)) (f2 n (cdr l)))
12.             (   (vl-string-right-trim " " (strcat (f2 (fix (/ n (caar l))) (cdr l)) " " (cadar l) " " (f2 (rem n (caar l)) (cdr l)))))
13.         )
14.     )
15.     (if (zerop n)
16.         "zero"
17.             (f2 n
18.                '(
19.                     (1e18 "quintillion")
21.                     (1e12 "trillion")
22.                     (1e09 "billion")
23.                     (1e06 "million")
24.                     (1e03 "thousand")
25.                     (1e02 "hundred")
26.                 )
27.             )
28.         )
29.     )
30. )

Words to Integer

Code - Auto/Visual Lisp: [Select]
1. (defun LM:words->int ( s / f1 f2 )
2.     (defun f1 ( s l / p )
3.         (cond
4.             (   (null l)
5.                 (cond
6.                     (   (vl-position (vl-string-trim " " s)
7.                            '("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen")
8.                         )
9.                     )
10.                     (   0   )
11.                 )
12.             )
13.             (   (not (setq p (vl-string-search (car l) s))) (f1 s (cdr l)))
14.             (   (+ (* 10 (- 10 (length l))) (f1 (substr s (+ 2 p (strlen (car l)))) (cdr l))))
15.         )
16.     )
17.     (defun f2 ( s l / p )
18.         (cond
19.             (   (null l) (f1 s '("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")))
20.             (   (not (setq p (vl-string-search (caar l) s))) (f2 s (cdr l)))
21.             (   (+ (* (f2 (substr s 1 p) (cdr l)) (cadar l)) (f2 (substr s (+ 2 p (strlen (caar l)))) (cdr l))))
22.         )
23.     )
24.     (if (= "zero" (strcase s t))
25.         0
26.         (f2 (strcase s t)
27.            '(
28.                 ("quintillion" 1e18)
30.                 ("trillion"    1e12)
31.                 ("billion"     1e09)
32.                 ("million"     1e06)
33.                 ("thousand"    1e03)
34.                 ("hundred"     1e02)
35.             )
36.         )
37.     )
38. )

Examples:

Code - Auto/Visual Lisp: [Select]
1. _\$ (rtos (LM:words->int (LM:int->words 3)) 2 0)
2. "3"
3. _\$ (rtos (LM:words->int (LM:int->words 31)) 2 0)
4. "31"
5. _\$ (rtos (LM:words->int (LM:int->words 314)) 2 0)
6. "314"
7. _\$ (rtos (LM:words->int (LM:int->words 3141)) 2 0)
8. "3141"
9. _\$ (rtos (LM:words->int (LM:int->words 31415)) 2 0)
10. "31415"
11. _\$ (rtos (LM:words->int (LM:int->words 314159)) 2 0)
12. "314159"
13. _\$ (rtos (LM:words->int (LM:int->words 3141592)) 2 0)
14. "3141592"
15. _\$ (rtos (LM:words->int (LM:int->words 31415926)) 2 0)
16. "31415926"
17. _\$ (rtos (LM:words->int (LM:int->words 314159265)) 2 0)
18. "314159265"
19. _\$ (rtos (LM:words->int (LM:int->words 3141592653)) 2 0)
20. "3141592653"
21. _\$ (rtos (LM:words->int (LM:int->words 31415926535)) 2 0)
22. "31415926535"
23. _\$ (rtos (LM:words->int (LM:int->words 314159265358)) 2 0)
24. "314159265358"
25. _\$ (LM:int->words 314159265358)
26. "three hundred fourteen billion one hundred fifty nine million two hundred sixty five thousand three hundred fifty eight"
27. _\$ (rtos (LM:words->int "three hundred fourteen billion one hundred fifty nine million two hundred sixty five thousand three hundred fifty eight") 2 0)
28. "314159265358"
« Last Edit: December 17, 2018, 06:03:54 PM by Lee Mac »