# "convert arabic numbers to words with javascript" Code Answer

4

you can do use similar function :

http://jsbin.com/jesoj/1/

zero to million :

function zerotomillion(num) {
num = (num + "").replace(" ", "");
var exceptional = {0: "zero", 11: "eleven", 12: "twelve",
13: "thirteen", 14: "fourteen", 15: "fifteen",
16: "sixteen", 17: "seventeen", 18: "eighteen",
19: "nineteen"};

var digit = ["", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"]; // don't add zero
var decade = ["", "ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"]; // don't add zero

var largenumber = ["hundred", "thousand", "million", "billion"];

var l = num.length-1,
a = (num[l]) ? num[l] : 0,
b = (num[l-1]) ? num[l-1] : 0,
ba = ""+b+a,
c = (num[l-2]) ? num[l-2] : 0,
d = (num[l-3]) ? num[l-3] : 0,
e = (num[l-4]) ? num[l-4] : 0,
ed = ""+e+d,
f = (num[l-5]) ? num[l-5] : 0,
num_ab = (exceptional[ba]) ? exceptional[ba] : (decade[b]+" "+digit[a]),
num_ed = (exceptional[ed]) ? exceptional[ed] : (decade[e]+" "+digit[d]),
name_c = (c!==0) ? (digit[c]+" "+largenumber)+" " : "",
name_d = (num_ed!==" ") ? (num_ed+" "+largenumber)+" " : "",
name_f = (f!==0) ? (digit[f]+" "+largenumber)+" " : "";
return name_f+name_d+name_c+num_ab;
}


zero to one hundred :

function zerotohundred(num) {
num = num+""; // .tostring();
var exceptional = {0: "zero", 11: "eleven", 12: "twelve",
13: "thirteen", 14: "fourteen", 15: "fifteen",
16: "sixteen", 17: "seventeen", 18: "eighteen",
19: "nineteen", 100: "one hundred"};

var digit = ["", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"]; // don't add zero
var decade = ["", "ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"]; // don't add zero

if (exceptional[num]) {
return exceptional[num];
} else {
var b = (num) ? num : num,
a = (num) ? num : 0;