Converting integers to English words

JavaScript (375)

Probably a terrible attempt, but anyway, here goes...

alert(function N(s,z){return O="zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thir,,fif,,,eigh,,,,twen,,for".split(","),(z?O[s]||O[s-10]||O[s-20]:s<13?N(s,1):s<20?N(s,1)+"teen":s<100?N(a=20+(s/10|0),1)+"ty"+(s%10?" "+N(s%10):""):s<1e3?N(s/100|0)+" hundred"+(s%100?" "+N(s%100):""):s<1e5?N(s/1e3|0)+" thousand"+(s%1e3?" "+N(s%1e3):""):0)||NaN}(prompt()))

Pretty-printed (as a function):

function N(s,z) {
  return O = "zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thir,,fif,,,eigh,,,,twen,,for".split(","),
      (z? O[s] || O[s-10] || O[s-20]
       : s < 13?  N(s,1)
       : s < 20?  N(s,1) + "teen"
       : s < 100? N(a=20+(s/10|0),1) + "ty" + (s%10?" "+N(s%10):"")
       : s < 1e3?  N(s/100|0) +  " hundred" + (s%100?" "+N(s%100):"")
       : s < 1e5?  N(s/1e3|0) + " thousand" + (s%1e3?" "+N(s%1e3):"") : 0) || NaN
}

Sample conversion (note that it even outputs NaN when out of bounds, i.e. invalid input):

540: five hundred forty
4711: four thousand seven hundred eleven
7382: seven thousand three hundred eighty two
1992: one thousand nine hundred ninety two
hutenosa: NaN
1000000000: NaN
-3: NaN

Mathematica 60 57

f = ToString@#~WolframAlpha~{{"NumberName", 1}, "Plaintext"} &

Usage:

f[500]

five hundred

Edit:

InputString[]~WolframAlpha~{{"NumberName", 1}, "Plaintext"}

Lisp, 72 56 characters

I realize 1) that this is old, and 2) that it relies entirely on the standard library to function, but the fact that you can get the c-lisp printing system to do this kind of thing has always impressed me. Also, this does in fact take the input from a user, convert it, and print it.

(format t "~:[NaN~;~:*~r~]" (parse-integer (read-line) :junk-allowed t))

It totals 72 characters.

  • :junk-allowed causes parse-integer to return nil on failure instead of raising an error.
  • ~:[if-nil~;if-non-nill] conditional predicated on nil, handles NaN where necessary
  • ~:* backs up the argument interpretation to re-consume the input
  • ~r prints the number as an english word string, as requested, except with full corrected punctuation

Sample:

17823658
seventeen million, eight hundred and twenty-three thousand, six hundred and fifty-eight

192hqfwoelkqhwef9812ho1289hg18hoif3h1o98g3hgq
NaN

Lisp info mainly from Practical Common Lisp.

Edit, golfed properly down to 56 characters

(format t "~:[NaN~;~:*~r~]"(ignore-errors(floor(read))))

This version works rather differently. Instead of reading a line and converting it, it invokes the lisp reader to interpret the input as a lisp s-expression, attempts to use it as a number, and if any errors are produced ignores them producing nil to feed the format string conditional. This may be the first instance I've seen of lisp producing a truly terse program... Fun!

  • (read) Invokes the lisp reader/parser to read one expression from standard input and convert it into an appropriate object
  • (floor) attempts to convert any numeric type into the nearest lower integer, non-numeric types cause it to raise an error
  • (ignore-errors ...) does what it says on the tin, it catches and ignores any errors in the enclosed expression, returning nil to feed the NaN branch of the format string

Tags:

Code Golf