Skip to content

Instantly share code, notes, and snippets.

@devn
Created January 27, 2012 22:31
Show Gist options
  • Save devn/1691304 to your computer and use it in GitHub Desktop.
Save devn/1691304 to your computer and use it in GitHub Desktop.
numbers to english in clojure
(ns quiz.core
(:require [clojure.pprint :as pp]))
(defn to-english [n]
(pp/cl-format nil "~@(~@[~R~]~^ ~A.~)" n))
(to-english 99999999999999999)
;=> "Ninety-nine quadrillion, nine hundred ninety-nine trillion, nine hundred ninety-nine billion, nine hundred ninety-nine million, nine hundred ninety-nine thousand, nine hundred ninety-nine"
(map to-english (range 0 101))
;=> ("Zero" "One" "Two" "Three" "Four" "Five" "Six" "Seven" "Eight" "Nine" "Ten" "Eleven" "Twelve" "Thirteen" "Fourteen" "Fifteen" "Sixteen" "Seventeen" "Eighteen" "Nineteen" "Twenty" "Twenty-one" "Twenty-two" "Twenty-three" "Twenty-four" "Twenty-five" "Twenty-six" "Twenty-seven" "Twenty-eight" "Twenty-nine" "Thirty" "Thirty-one" "Thirty-two" "Thirty-three" "Thirty-four" "Thirty-five" "Thirty-six" "Thirty-seven" "Thirty-eight" "Thirty-nine" "Forty" "Forty-one" "Forty-two" "Forty-three" "Forty-four" "Forty-five" "Forty-six" "Forty-seven" "Forty-eight" "Forty-nine" "Fifty" "Fifty-one" "Fifty-two" "Fifty-three" "Fifty-four" "Fifty-five" "Fifty-six" "Fifty-seven" "Fifty-eight" "Fifty-nine" "Sixty" "Sixty-one" "Sixty-two" "Sixty-three" "Sixty-four" "Sixty-five" "Sixty-six" "Sixty-seven" "Sixty-eight" "Sixty-nine" "Seventy" "Seventy-one" "Seventy-two" "Seventy-three" "Seventy-four" "Seventy-five" "Seventy-six" "Seventy-seven" "Seventy-eight" "Seventy-nine" "Eighty" "Eighty-one" "Eighty-two" "Eighty-three" "Eighty-four" "Eighty-five" "Eighty-six" "Eighty-seven" "Eighty-eight" "Eighty-nine" "Ninety" "Ninety-one" "Ninety-two" "Ninety-three" "Ninety-four" "Ninety-five" "Ninety-six" "Ninety-seven" "Ninety-eight" "Ninety-nine" "One hundred")
@AlexBaranosky
Copy link

I counter with :

(defn ordinalize [int]
  (if (contains? #{11 12 13} (mod int 100))
    (str int "th")
    (condp = (mod int 10)
      1 (str int "st")
      2 (str int "nd")
      3 (str int "rd")
      (str int "th"))))

(ordinalize 3)
;; => "3rd"

(ordinalize 113)
;; => "113th"

@devn
Copy link
Author

devn commented Jan 31, 2012

Not too shabby. cl-format includes that as well, though!

(map #(pp/cl-format nil "~:R" %) (range 0 25))
("zeroth" "first" "second" "third" "fourth" "fifth" ... "twenty-fourth")

@AlexBaranosky
Copy link

I did some work this past week using cl-format, no doubt it is.... awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment