SyntaxHighlighter

Saturday, 30 July 2011

Converting Numbers to Words in Javascript

I do not like using numeric characters in prose. In my opinion, writing looks better and reads more easily when numbers are spelled out in words.

I have written a Javascript class to enable my web applications to print numbers as words. The code may be useful to others I think, and so I have written this blog post.

The code linked to below will convert to words, any integer in the range [0, 1034 - 1].

The code could easily be extended to work with even larger numbers, but as numbers grow very large, the practicality of writing them as words diminishes. 1034 is of course overkill, and the fact that I choose to use scientific notation to express it implies that even I, a man who spells out numbers as words even in SMS messages, think that that number should not be written in words.

Up to the limit I decided on though, the value proposition of adding a single word (eg. 'heptillion') to the code and thereby increasing its range of application a thousandfold was just too much for me to resist. My resistance kicked in at 1034 for no better reasons than that the range had to end somewhere, that a 'decillion' is a nice round number, and that the word 'decillion' seems to have an air of finality to it, perhaps due to similarity with other words 'decease', 'decay', 'decapitate' etc.

See Names of Large Numbers in Wikipedia.

Link to Code

The code is too long to include in this post, so I go one better than Pierre de Fermat, and provide a link to it here. The file linked to has no dependencies.

Digression

A fact I found interesting while researching the origin of the word 'decillion' is that the word 'decimate' has a specific and gruesome meaning. The word 'decimate' refers to 'the practice of punishing mutinous military units by capital execution of one in every 10, by lot. Killing one in ten, chosen by lots, from a rebellious city or a mutinous army was a common punishment in classical times.' (Reference: Online Etymology Dictionary.) Note that the Online Etymology Dictionary does not follow my suggested rules for the output of numbers in sentences.

Test Results

1 one
10 ten
100 one hundred
1000 one thousand
10000 ten thousand
100000 one hundred thousand
1000000 one million
10000000 ten million
100000000 one hundred million
1000000000 one billion
10000000000 ten billion
9 nine
99 ninety-nine
999 nine hundred and ninety-nine
9999 nine thousand, nine hundred and ninety-nine
99999 ninety-nine thousand, nine hundred and ninety-nine
999999 nine hundred and ninety-nine thousand, nine hundred and ninety-nine
9999999 nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
99999999 ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
999999999 nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
9999999999 nine billion, nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
99999999999 ninety-nine billion, nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine
2 two
12 twelve
112 one hundred and twelve
2112 two thousand, one hundred and twelve
12112 twelve thousand, one hundred and twelve
112112 one hundred and twelve thousand, one hundred and twelve
2112112 two million, one hundred and twelve thousand, one hundred and twelve
12112112 twelve million, one hundred and twelve thousand, one hundred and twelve
112112112 one hundred and twelve million, one hundred and twelve thousand, one hundred and twelve
2112112112 two billion, one hundred and twelve million, one hundred and twelve thousand, one hundred and twelve
12112112112 twelve billion, one hundred and twelve million, one hundred and twelve thousand, one hundred and twelve
5 five
50 fifty
505 five hundred and five
5050 five thousand and fifty
50505 fifty thousand, five hundred and five
505050 five hundred and five thousand and fifty
5050505 five million, fifty thousand, five hundred and five
50505050 fifty million, five hundred and five thousand and fifty
505050505 five hundred and five million, fifty thousand, five hundred and five
5050505050 five billion, fifty million, five hundred and five thousand and fifty
50505050505 fifty billion, five hundred and five million, fifty thousand, five hundred and five
2 two
22 twenty-two
202 two hundred and two
2002 two thousand and two
20002 twenty thousand and two
200002 two hundred thousand and two
2000002 two million and two
20000002 twenty million and two
200000002 two hundred million and two
2000000002 two billion and two
20000000002 twenty billion and two

No comments:

Post a Comment