Jump to content

Convert numbers to words. Function from Lee Mac. Question...


dilan

Recommended Posts

Hello everyone. I found this code in the forum:

(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")
                )
            )
        )
    )
)

Tell me please who knows how to make words-> int.
For example:
From "twenty one thousand five hundred thirty seven" in 21537

Link to comment
Share on other sites

I think the trick here is to convert the text number to lists. If you look at Lee Macs String to List (LM:STR->LST) function that might help.

 

Make up 7 lists, one for each power of 10 description ("quintillion" "quadrillion" "trillion" "billion" "million" "thousand" "hundred" "tens")

Use Lee Macs LM:STR-LST and have the deliminator as the power description. Also check if the number is that big and if not put a blank in that position.

Then move on to create the next power list, using item 2 of the list before

 

So in your example, in the list

'Thousand' if would be ("Twenty One" "five hundred thirty seven")

'Hundred would be ("Five" "thirty seven")

'Tens' would be ("thirty seven" "")

 

Then you can work it all out from the descriptions. You will need a loop in there in case for example you have three hundred two thousand five hundred and thirty seven.

 

Maybe that will get you started?

 

Link to comment
Share on other sites

5 hours ago, Steven P said:

I think the trick here is to convert the text number to lists. If you look at Lee Macs String to List (LM:STR->LST) function that might help.

 

Make up 7 lists, one for each power of 10 description ("quintillion" "quadrillion" "trillion" "billion" "million" "thousand" "hundred" "tens")

Use Lee Macs LM:STR-LST and have the deliminator as the power description. Also check if the number is that big and if not put a blank in that position.

Then move on to create the next power list, using item 2 of the list before

 

So in your example, in the list

'Thousand' if would be ("Twenty One" "five hundred thirty seven")

'Hundred would be ("Five" "thirty seven")

'Tens' would be ("thirty seven" "")

 

Then you can work it all out from the descriptions. You will need a loop in there in case for example you have three hundred two thousand five hundred and thirty seven.

 

Maybe that will get you started?

 

Thank you very much for the answers.

Link to comment
Share on other sites

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"

 

Edited by Lee Mac
  • Like 1
Link to comment
Share on other sites

2 hours ago, Lee Mac said:

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"

 

Thank you very much, Lee.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...