Created
January 27, 2012 22:31
-
-
Save devn/1691304 to your computer and use it in GitHub Desktop.
numbers to english in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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") |
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"
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")
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
Nice... that could come in handy sometime.