Last active
August 8, 2024 15:14
-
-
Save devm33/8008b37baa983ac2e0ed2fb0db24d477 to your computer and use it in GitHub Desktop.
English Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert numbers to english words | |
const digit = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; | |
const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']; | |
const tens = [null, null, 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']; | |
const orders = ['hundred', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion']; | |
function floor(number, order) { | |
return Math.floor(number / order) | |
} | |
function english(number, recur) { | |
if(number == 0 && recur) { | |
return ''; | |
} | |
let p = recur ? ' ' : ''; | |
if(number < 10) { | |
return p + digit[number]; | |
} | |
if(number < 20) { | |
return p + teens[number - 10]; | |
} | |
if(number < 1e2) { | |
return p + tens[floor(number, 10)] + english(number % 10, true); | |
} | |
let div = 1e2; | |
let max = 1e3; | |
let ord = 0; | |
while(number >= max) { | |
ord++; | |
div = max; | |
max = max * 1e3; | |
} | |
return p + english(floor(number, div)) + ' ' + orders[ord] + english(number % div, true); | |
} | |
// Testing | |
const assert = require('assert'); | |
var under_hundred = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty', 'thirty one', 'thirty two', 'thirty three', 'thirty four', 'thirty five', 'thirty six', 'thirty seven', 'thirty eight', 'thirty nine', 'forty', 'forty one', 'forty two', 'forty three', 'forty four', 'forty five', 'forty six', 'forty seven', 'forty eight', 'forty nine', 'fifty', 'fifty one', 'fifty two', 'fifty three', 'fifty four', 'fifty five', 'fifty six', 'fifty seven', 'fifty eight', 'fifty nine', 'sixty', 'sixty one', 'sixty two', 'sixty three', 'sixty four', 'sixty five', 'sixty six', 'sixty seven', 'sixty eight', 'sixty nine', 'seventy', 'seventy one', 'seventy two', 'seventy three', 'seventy four', 'seventy five', 'seventy six', 'seventy seven', 'seventy eight', 'seventy nine', 'eighty', 'eighty one', 'eighty two', 'eighty three', 'eighty four', 'eighty five', 'eighty six', 'eighty seven', 'eighty eight', 'eighty nine', 'ninety', 'ninety one', 'ninety two', 'ninety three', 'ninety four', 'ninety five', 'ninety six', 'ninety seven', 'ninety eight', 'ninety nine'] | |
for(let i = 0; i < 100; i++) { | |
assert.equal(english(i), under_hundred[i]); | |
} | |
assert.equal(english(100), 'one hundred'); | |
assert.equal(english(201), 'two hundred one'); | |
assert.equal(english(411), 'four hundred eleven'); | |
assert.equal(english(921), 'nine hundred twenty one'); | |
assert.equal(english(1203), 'one thousand two hundred three'); | |
assert.equal(english(3091), 'three thousand ninety one'); | |
assert.equal(english(5555), 'five thousand five hundred fifty five'); | |
assert.equal(english(12345), 'twelve thousand three hundred forty five'); | |
assert.equal(english(987654), 'nine hundred eighty seven thousand six hundred fifty four'); | |
assert.equal(english(1002001), 'one million two thousand one'); | |
assert.equal(english(56103045), 'fifty six million one hundred three thousand forty five'); | |
assert.equal(english(56103045), 'fifty six million one hundred three thousand forty five'); | |
assert.equal(english(1e18), 'one quintillion'); |
Hi @devm33, just wanted to flag that this Gist has the number 19 listed as "ninety" instead of "nineteen" in both the code and the test cases!
oh lol thanks :) fixed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @devm33, just wanted to flag that this Gist has the number 19 listed as "ninety" instead of "nineteen" in both the code and the test cases!