Skip to content

Instantly share code, notes, and snippets.

@bcamarda
Created June 27, 2012 03:15
Show Gist options
  • Save bcamarda/3001102 to your computer and use it in GitHub Desktop.
Save bcamarda/3001102 to your computer and use it in GitHub Desktop.
Numbers in Words in JS
var inWords = function(number){
var numberHash = {
0:"", 1:"one", 2:"two", 3:"three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten",
11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen", 18: "eighteen",
19: "nineteen", 20: "twenty", 30: "thirty", 40: "forty", 50: "fifty", 60: "sixty", 70: "seventy", 80: "eighty", 90: "ninety"
};
if(number <= 20){
return numberHash[number];
}
else if(20 < number && number < 100){
var tens = Math.floor(number / 10) * 10;
var ones = number % 10;
return numberHash[tens] + " " + numberHash[ones];
}
else{
var hundreds = Math.floor(number / 100) * 100;
if(number - hundreds <= 20){
return numberHash[(hundreds/100)] + " hundred " + numberHash[(number-hundreds)];
}
else{
var tens = (Math.floor((number - hundreds) / 10)) * 10;
var ones = (number - hundreds) % 10;
return numberHash[(hundreds/100)] + " hundred " + numberHash[tens] + " " + numberHash[ones];
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment