Writing a number in English


#1

Just for fun, here’s a Lisp program to write a number in English:

(defun english (n)
  (let ((t10 (truncate n 10)) (m10 (mod n 10))
        (t100 (truncate n 100)) (m100 (mod n 100))
        (t1000 (truncate n 1000)) (m1000 (mod n 1000)))
    (cond
     ((not (<= 0 n 999999)) 
      (error "n must be between 0 and 999999"))
     ((< n 10)
      (nth n '("zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine")))
     ((< n 20)
      (nth (- n 10) '("ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen"
                      "sixteen" "seventeen" "eighteen" "nineteen")))
     ((and (< n 100) (zerop m10))
      (nth (- t10 2) '("twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty"
                       "ninety")))
     ((< n 100)
      (format nil "~a-~a" (english (* t10 10)) (english m10)))
     ((and (< n 1000) (zerop m100))
      (format nil "~a hundred" (english t100)))
     ((< n 1000)
      (format nil "~a hundred and ~a" (english t100) (english m100)))
     ((zerop m1000)
      (format nil "~a thousand" (english t1000)))
     (t
      (format nil "~a thousand ~a~a" 
              (english t1000) (if (< m1000 100) "and " "") (english m1000))))))

It works for integers between 0 and 999999. For example:

> (english 999999)
"nine hundred and ninety-nine thousand nine hundred and ninety-nine"

I originally wrote it as a way of generating a lot of text to test uLisp functions, such as the string handling and SD card interface.

In trying to make it as compact as possible I thought about handling the suffixes “teen” and “ty” separately, but decided that there are too many special cases (such as “eighty”) and it would probably work out longer.

Incidentally, writing this made me appreciate some oddities about the English language:

  • Why do we spell “four” and “fourteen” with a ‘u’, but “forty” without?

  • Why do we include “and” in “one thousand and twenty-three” but not “one thousand two hundred”?


#2

Why do we spell “four” and “fourteen” with a ‘u’, but “forty” without?

Maybe Americans learned to count to forty before the Brit’s and kept them from adding that stupid ‘u’ after the ‘o’ in certain words. :)

Why do we include “and” in “one thousand and twenty-three” but not “one thousand two hundred”?

I don’t and was taught not to by my 8th grade math teacher. Alas, that was <cough>fiddy<cough> odd years ago. But again, I’m over on this side of the Pond. :)