Skip to content

Instantly share code, notes, and snippets.

@kdimatteo
Last active August 12, 2016 20:10
Show Gist options
  • Save kdimatteo/c6efe3a7f0ca377f98551da0cdf4e6a5 to your computer and use it in GitHub Desktop.
Save kdimatteo/c6efe3a7f0ca377f98551da0cdf4e6a5 to your computer and use it in GitHub Desktop.
numToWords (wcag)
function numToWords (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'];
if ((num = num.toString()).length > 9) {
return 'num overflow';
}
var n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!n) return;
var str = '';
str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) : '';
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment