Given any integer print an English phrase that describes the integer. (e.g. "One Thousand, Three Hundred Twenty Four").
A Pen by Chris Hicks on CodePen.
| <p>Given any integer print an English phrase that describes the integer. (e.g. "One Thousand, Three Hundred Twenty Four").</p> | |
| <div id="log"></div> |
Given any integer print an English phrase that describes the integer. (e.g. "One Thousand, Three Hundred Twenty Four").
A Pen by Chris Hicks on CodePen.
| function log(text) { | |
| $('#log').append(text + "<br>\n"); | |
| } | |
| function int2Eng(num) { | |
| 'use strict'; | |
| var | |
| ones = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"], | |
| teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"], | |
| tens = ["", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"], | |
| numberGroups = ["", "Thousand", "Million", "Billion"], | |
| /* Convert 12300 to 300,12 so we can work with the order of magnitudes easier. | |
| * This makes 300 the first coma separated group, so we know not to append a | |
| * number group like 'Thousand' or 'Million'. 12 is the second group, so we | |
| * know it will be in the 'Thousand' range and we can append that to the | |
| * phrase 'Twelve' to get 'Tweleve Thousand' | |
| */ | |
| reversed = reverseStr(num.toString()), | |
| groups = reversed.match(/\d{1,3}/g),group, | |
| output = []; | |
| // Blow up if not a number | |
| // Blow up if negative | |
| // Blow up if greater than 999,999,999,999 | |
| log(" "); | |
| log('num: ' + num); | |
| // Use groups length to count down the order of magnitude. | |
| // Print number for 12, group 1 -> Tweleve Thousand | |
| // Print number for 300, group 0 -> Three Hundred | |
| for(group = groups.length - 1; group >= 0; group--) { | |
| output.push(printNumber(reverseStr(groups[group]), group)); | |
| } | |
| // join the two phrases together... | |
| log(output.join(', ')); | |
| function printNumber (number, numberGroup) { | |
| var length = number.length, | |
| output = []; | |
| if (length === 1) { | |
| output.push(singleDigit(number)); | |
| } | |
| if (length === 2) { | |
| output.push(doubleDigit(number)); | |
| } | |
| if (length === 3) { | |
| output.push(tripleDigit(number)); | |
| } | |
| output.push(numberGroups[numberGroup]) | |
| return output.join(' '); | |
| } | |
| function singleDigit(number) { | |
| return ones[number.charAt(0)]; | |
| } | |
| function doubleDigit(number) { | |
| var output = []; | |
| if (number == "00") { // We don't say Zero Zero or Five Hundred Zero | |
| return ''; | |
| } else if (number.charAt(0) === '1') { // Cover the things between 9 and 20 | |
| output.push(teens[number.charAt(1)]); | |
| } else { // Everything else is easy. | |
| output.push(tens[number.charAt(0)]); | |
| output.push(ones[number.charAt(1)]); | |
| } | |
| return output.join(' '); | |
| } | |
| function tripleDigit(number) { | |
| var output = []; | |
| output.push(singleDigit(number.substring(0,1))); | |
| output.push("Hundred"); | |
| output.push(doubleDigit(number.substring(1,3))); | |
| return output.join(' '); | |
| } | |
| function reverseStr(str) { | |
| return str.split('').reverse().join(''); | |
| } | |
| } | |
| int2Eng(1); // One | |
| int2Eng(10); // Ten | |
| int2Eng(12); // Twelve | |
| int2Eng(21); // Twenty One | |
| int2Eng(123); // One Hundred Twenty Three | |
| int2Eng(300); // Three Hundred | |
| int2Eng(301); // Three Hundred One | |
| int2Eng(1234); // One Thousand, Two Hundred Thirty Four | |
| int2Eng(12345); // Twelve Thousand, Three Hundred Fourty Five | |
| int2Eng(12312); // Twelve Thousand, Three Hundred Twelve | |
| int2Eng(123456); // One Hundred Twenty Three Thousand, Four Hundred Fifty Six | |
| int2Eng(3744892348); // Three Billion, Seven Hundred Fourty Four Million, Eight Hundred Ninety Two Thousand, Three Hundred Fourty Eight | |