Skip to content

Instantly share code, notes, and snippets.

@rfprod
Created January 27, 2017 11:14
Show Gist options
  • Save rfprod/277487449b6adc44a0d3b0738065c8a4 to your computer and use it in GitHub Desktop.
Save rfprod/277487449b6adc44a0d3b0738065c8a4 to your computer and use it in GitHub Desktop.
Number 2 Words
function number2words(n) {
// works for numbers between 0 and 999999
console.log('input:', n);
const map = {
'singleUnits': ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
'doubleUnits': ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
'tenths': ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
};
let output;
const t = Math.floor(n / 1000); // thousands
const h = Math.floor((n - t * 1000) / 100); // hundreds
const u = n - t * 1000 - h * 100; // units
console.log('thousands:', t);
console.log('hundreds:', h);
console.log('units:', u);
function getUnits(num) {
let output = (num < 10) ? map.singleUnits[num] : (num % 10 === 0) ? map.tenths[num / 10 - 1] : (num < 20) ? map.doubleUnits[num % 10 - 1] : null;
if (output === null) {
const tenths = Math.floor(num / 10);
const units = num % 10;
output = map.tenths[tenths - 1] + '-' + map.singleUnits[units];
}
return output;
}
const unitsStr = getUnits(u);
console.log('unitsStr:', unitsStr);
const hundredsStr = (h) ? map.singleUnits[h] + ' hundred ' : null;
console.log('hundredsStr:', hundredsStr);
let thousandsStr = (t > 0 && t < 10) ? map.singleUnits[t] + ' thousand ' : null;
if (thousandsStr === null) {
const hundreds = Math.floor(t / 100);
const tenths = Math.floor((t - hundreds * 100) / 10);
const units = t - hundreds * 100 - tenths * 10;
console.log('thousands decomp:', hundreds, tenths, units);
thousandsStr = '';
if (hundreds) { thousandsStr += map.singleUnits[hundreds] + ' hundred '; }
if (tenths && units) { thousandsStr += (tenths < 2) ? map.doubleUnits[units - 1] + ' thousand ' : map.tenths[tenths - 1] + '-' + map.singleUnits[units] + ' thousand '; }
else if (tenths && !units) { thousandsStr += map.tenths[tenths - 1] + ' thousand '; }
else if (!tenths && units) { thousandsStr += map.singleUnits[units] + ' thousand '; }
else if (!tenths && !units && thousandsStr) { thousandsStr += 'thousand '; }
}
console.log('thousandsStr:', thousandsStr);
output = unitsStr;
console.log('output, unitsStr:', unitsStr);
if (hundredsStr && hundredsStr !== 'zero') { output = (output !== 'zero') ? hundredsStr + output : hundredsStr; }
if (thousandsStr && thousandsStr !== 'zero') { output = (output !== 'zero') ? thousandsStr + output : thousandsStr; }
return output.trim();
}
// TEST
number2words(0); // "zero"
number2words(10); // "ten"
number2words(19); // "nineteen"
number2words(22); // "twenty-two"
number2words(80); // "eighty"
number2words(100); // "one hundred"
number2words(301); // "three hundred one"
number2words(99999); // "ninety-nine thousand nine hundred ninety-nine"
number2words(888888); // "eight hundred eighty-eight thousand eight hundred eighty-eight"

Number 2 Words

Function number2words(n) returns number represented as a string of words.

Example: number2words(888888) returns "eight hundred eighty-eight thousand eight hundred eighty-eight"

Scripts by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment