Javascript function to convert a number into the English written format (1=ONE, 2=TWO, etc.)
This takes any positive integer number (up to 10^66-1) and returns the English written format (in upper case) for that number. So 1 -> ONE, 666 -> SIX HUNDRED AND SIXTY SIX.
function inWords(num) {
var a = ["","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN "];
var b = ["","","TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"];
var c = ["THOUSAND","MILLION","BILLION","TRILLION","QUADRILLION","QUINTILLION","SEXTILLION","SEPTILLION","OCTILLION","NONILLION","DECILLION","UNDECILLION","DUODECILLION",
"TREDECILLION","QUATTUORDECILLION","QUINDECILLION","SEXDECILLION","SEPTEN-DECILLION","OCTODECILLION","NOVEMDECILLION","VIGINTILLION"];
if (num==0) return "";
if (num<20) return a[num].trim();
if (num<100) return (b[Math.floor(num/10)] + " " + a[num%10]).trim();
if (num<1000) return (a[Math.floor(num/100)] + " HUNDRED" + (num%100&&num%100<100?" AND ":" ") + inWords(num%100)).trim();
for (var i=0; i<c.length; i++) {
var name = c[i];
var lower = Math.pow(10,3+i*3);
var upper = lower*1000;
var test = num%lower;
if (num<upper) return (inWords(Math.floor(num/lower)) + " " + name + (test&&test<100?" AND ":" ") + inWords(test)).trim();
}
throw "Too large for now";
}
function inWords(num) {
var a = ["","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN "];
var b = ["","","TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"];
var c = ["THOUSAND","MILLION","BILLION","TRILLION","QUADRILLION","QUINTILLION","SEXTILLION","SEPTILLION","OCTILLION","NONILLION","DECILLION","UNDECILLION","DUODECILLION",
"TREDECILLION","QUATTUORDECILLION","QUINDECILLION","SEXDECILLION","SEPTEN-DECILLION","OCTODECILLION","NOVEMDECILLION","VIGINTILLION"];
if (num==0) return "";
if (num<20) return a[num].trim();
if (num<100) return (b[Math.floor(num/10)] + " " + a[num%10]).trim();
if (num<1000) return (a[Math.floor(num/100)] + " HUNDRED" + (num%100&&num%100<100?" AND ":" ") + inWords(num%100)).trim();
for (var i=0; i<c.length; i++) {
var name = c[i];
var lower = Math.pow(10,3+i*3);
var upper = lower*1000;
var test = num%lower;
if (num<upper) return (inWords(Math.floor(num/lower)) + " " + name + (test&&test<100?" AND ":" ") + inWords(test)).trim();
}
throw "Too large for now";
}