Any downloadable material on this site is provided as is. If something bad happens, Andrew Hoyer will in no way take any responsibility (though he would most definitely send his heart felt remorse). © 2010 Andrew Hoyer
Type nearly any positive integer (maximum of 66 digits) in the text box below to see it converted into its word counterpart.
I had seen this done before (just do a google search for 'numbers to words javascript' and you'll see solutions all over the place, but it still seemed like a cool idea to do myself. In the end I finished with 50 lines of very readable, very pretty, recursive code.
Sure thats fine and dandy but I thought I could take it further. After some mulling about and a bit of tinkering I was able to get it down to only 8 lines but at the expense of any form of readability. I did this through the (over) usage of the (condition) ? true : false; shorthand for if (condition){ true } else { false }. By using this I was able to get the brunt of the conversion done in only 2 lines.
Heres what the source looks like (I've hard wrapped the lines so that they don't go shooting off the screen (It's not like its readable in the first place):
1. var names =[{"0":"zero","1":"one","2":"two","3":"three","4":"fou
r","5":"five","6":"six","7":"seven","8":"eight","9":"nine"},
{"0":"ten","1":"eleven","2":"twelve","3":"thirteen","4":"fou
rteen","5":"fifteen","6":"sixteen","7":"seventeen","8":"eigh
teen","9":"nineteen"},{"2":"twenty","3":"thirty","4":"fourty
","5":"fifty","6":"sixty","7":"seventy","8":"eighty","9":"ni
nety"},["","thousand","million","billion","trillion","quadri
llion","quintillion","sextillion","septillion","octillion","
nonillion","decillion","undecillion","duodecillion","tredeci
llion","quattuordecillion", "quindecillion","sexdecillion","
septdecillion","octdecillion","novemdecillion","vigintillion"
]];
2. function go(){
3. document.getElementById("output").innerHTML = (to_words((doc
ument.getElementById("input").value.replace(/[^0-9]/g,''
)).split("").reverse(),0));
4. }
5. function to_words(s, n){
6. var ns = s.slice(0,3);
7. return (ns.length < 1)?"":to_words(s.slice(3,s.length),n+1)+
((ns.length>1)?((ns.length==3&&ns[2]!="0" )?names[0][ns[
2]]+" hundred "+((ns[1]=="1")?names[1][ns[0]]+" ":(ns[1]
!="0")?names[2][ns[1]]+" "+((ns[0]!="0")?names[0][ns[0]]
+" ":""):(ns[0]!="0"?names[0][ns[0]]+" ":"")):((ns[1]=="
1")?names[1][ns[0]]+" ":(ns[1]!="0")?names[2][ns[1]]+" "
+((ns[0]!="0")?names[0][ns[0]]+" ":""):(ns[0]!="0"?names
[0][ns[0]]+" ":""))) +(((ns.length==3&&(ns[0]!="0"||ns[1
]!="0"||ns[2]!="0"))||(ns.length==2&&(ns[0]!="0"||ns[1]!
="0"))||(ns.length==1&&ns[0]!="0"))?"<span class='magnit
ude'>"+names[3][n]+"</span> ":""):((ns.length==1&&ns[0]!
="0")?names[0][ns[0]]+" ":"") + (((ns.length==3&&(ns[0]!
="0"||ns[1]!="0"||ns[2]!="0"))||(ns.length==2&&(ns[0]!="
0"||ns[1]!="0"))||(ns.length==1&&ns[0]!="0"))?"<span cla
ss='magnitude'>"+names[3][n]+"</span> ":""));
8. }
If for some reason you come across an incredibly large number such as: 6,670,903,752,021,072,936,960. You now know that this number is in fact "six sextillion six hundred seventy quintillion nine hundred three quadrillion seven hundred fifty two trillion twenty one billion seventy two million nine hundred thirty six thousand nine hundred sixty"... This as it turns out is the number of possible sudoku puzzles.