javascript number to words

Solutions on MaxInterview for javascript number to words by the best coders in the world

showing results for - "javascript number to words"
Michele
15 Apr 2018
1var num = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" ");
2var tens = "twenty thirty forty fifty sixty seventy eighty ninety".split(" ");
3
4function number2words(n){
5    if (n < 20) return num[n];
6    var digit = n%10;
7    if (n < 100) return tens[~~(n/10)-2] + (digit? "-" + num[digit]: "");
8    if (n < 1000) return num[~~(n/100)] +" hundred" + (n%100 == 0? "": " and " + number2words(n%100));
9    return number2words(~~(n/1000)) + " thousand" + (n%1000 != 0? " " + number2words(n%1000): "");
10}