d3-format
d3-format copied to clipboard
Convert Numbers to Words
Perhaps out of scope, but an interesting challenge: convert numbers to words.
Inspired by the StackOverflow: Indian Numbering System. How to Convert into the Official and Common-Use Numbering Systems Words/Strings.
For example, English locale:
test.equal(f(0), "zero");
test.equal(f(1), "one");
test.equal(f(2), "two");
test.equal(f(3), "three");
test.equal(f(4), "four");
test.equal(f(5), "five");
test.equal(f(6), "six");
test.equal(f(7), "seven");
test.equal(f(8), "eight");
test.equal(f(9), "nine");
test.equal(f(10), "ten");
test.equal(f(11), "eleven");
...
test.equal(f(99), "ninety nine");
test.equal(f(100), "one hundred");
test.equal(f(101), "one hundred and one");
test.equal(f(123), "one hundred and twenty three");
test.equal(f(1000), "one thousand");
test.equal(f(1101), "one thousand one hundred and one");
test.equal(f(10000), "ten thousand");
test.equal(f(100000), "one hundred thousand");
test.equal(f(1000000), "one million");
test.equal(f(10000000), "one billion");
test.equal(f(100000000), "one trillion");
For example, Indian locale (from StackOverflow: Indian Numbering System. How to Convert into the Official and Common-Use Numbering Systems Words/Strings):
test.equal(f(50), "fifty");
test.equal(f(12000), "twelve thousand");
test.equal(f(777000), "seven lakh seventy seven thousand");
test.equal(f(550001), "five lakh fifty thousand one");
test.equal(f(12345678) "one crore twenty three lakh forty five thousand six hundred seventy eight");
test.equal(f(123456789) "twelve crore thirty four lakh fifty six thousand seven hundred eighty nine");
test.equal(f(1234567890) "one hundred twenty three crore forty five lakh sixty seven thousand eight hundred ninety");
test.equal(f(12345678900) "one thousand two hundred thirty four crore fifty six lakh seventy eight thousand nine hundred");
Note: I've modified the original test cases by:
- making everything lower case,
- removing commas, and
- removing dashes between numbers.
I'm not sure if those are standard practices in India (they might be), but they seemed off to me. Input welcome!
Very interesting