Skip to content

Instantly share code, notes, and snippets.

@manuelvanrijn
Created October 12, 2011 14:31
Show Gist options
  • Save manuelvanrijn/1281361 to your computer and use it in GitHub Desktop.
Save manuelvanrijn/1281361 to your computer and use it in GitHub Desktop.
number to human readable text
function readable_number(num){
var bits_a = ["thousand", "million", "billion", "trillion", "zillion"];
var bits_b = ["ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"];
var bits_c = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
if (num==0){return 'zero';}
var result = (num<0)?'minus ':'';
num = Math.abs(num);
for(var i=bits_a.lenght; i>0; i--){
var p = pow(1000, i);
if (num > p){
var b = Math.floor(num/p);
num -= p * b;
result += readable_number(b) + ' ' + bits_a[i-1];
result += ((num)?', ':'');
}
}
if (num > 100){
var b = Math.floor(num/100);
num -= 100 * b;
result += readable_number(b) + ' hundred' + ((num)?' and ':' ');
}
if (num >= 20){
var b = Math.floor(num/10);
num -= 10 * b;
result += bits_b[b-1] + ' ';
}
if (num){
result += bits_c[num-1];
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment