The goal of programming is the multiobjective optimization, we write code to automatize things that would take some time to do them manually. For many projects, you will need to convert a number to its words representation to prevent that the user writes the entire number into words (e.g the number 123456789 with words would be one hundred twenty-three million, four hundred fifty-six thousand, seven hundred eighty-nine). Yeah, that's a really long number and i know that you didn't even read the entire number, so, why would your user want to write it by himself ? Make this task easy for your users by automating it.
In this article you will learn to convert numbers to its words representation either with a library or using a single function.
A. Use a library
You can install this module with npm executing (as this module is registered on npm, it works perfectly with Node.js too):
npm install number-to-words
Or if you use bower:
bower install number-to-words
numberToWords variable will be globally accesible in the document and then include the
numberToWords.min.js file in your document using a script tag:
If you want more information about this project, visit the official repository in Github here.
This library only converts integers numbers, any float value will be automatically parsed to integer.
The library provides 3 methods that allow you to convert an integer (or a number in string type) into words. Remember that you need to require the module in case you're using requireJS:
// If you're using Node.js or browserify (requireJS) // require the module using: var numberToWords = require('number-to-words'); numberToWords.toWords(13); // => “thirteen” // If you're using the script in the browser // then the global "numberToWords" variable will be accesible in the window numberToWords.toOrdinal(1); numberToWords.toWordsOrdinal(1); numberToWords.toWords(1);
1. Converting a number to its abbreviated ordinal
You can convert the numbers to ordinals easily with this library, an ordinal number defines the position of something in a series, such as first, second, or third. Ordinal numbers are used as adjectives, nouns, and pronouns.
To convert a number to an abbreviated ordinal use the
// 1st numberToWords.toOrdinal(1); // 2nd numberToWords.toOrdinal(2); // 3rd numberToWords.toOrdinal(3); // 1500th numberToWords.toOrdinal(1500); // 1234567890th numberToWords.toOrdinal(1234567890);
The method return a string with the abbreviate ordinal value of the providen integer (or string) as first parameter.
2. Converting a number to its ordinal in words
Instead of use an abbreviation, numberToWords allows you to convert a number to an ordinal in words using the
// first numberToWords.toWordsOrdinal(1); // second numberToWords.toWordsOrdinal(2); // third numberToWords.toWordsOrdinal(3); // one thousand, five hundredth numberToWords.toWordsOrdinal(1500); // one billion, two hundred thirty-four million, five hundred sixty-seven thousand, eight hundred ninetieth numberToWords.toWordsOrdinal(1234567890);
Pretty awesome and easy, isn't ?
3. Converting a number into words
You can convert a number easily to its words representation using the
// one numberToWords.toWords(1); // two numberToWords.toWords(2); // three numberToWords.toWords(3); // one thousand, five hundred numberToWords.toWords(1500); // one billion, two hundred thirty-four million, five hundred sixty-seven thousand, eight hundred ninety numberToWords.toWords(1234567890);
This library supports the conversion up to 999999999999999 (15-digit number).
The usage of the library with a bigger number will inevitably lead to an exception Uncaught RangeError: Maximum call stack size exceeded that you can easily prevent using a try/catch statement.
B. Self implementation
If you don't want to use a library to achieve your goal and you only want to convert numbers to words using a simple snippet, then you can use the following simple function:
And you can use easily:
// one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine numberToEnglish(123456789); // Use a custom separator (like , instead of "and") // one hundred , twenty three million four hundred , fifty six thousand seven hundred , eighty nine numberToEnglish(123456789, ",");
In the same way that the library, only a digit up to 15-digit is allowed. However, in this case this function won't throw an exception but the values that will generate in words will be not correct. Besides, this implementation doesn't support the conversion to ordinal.
Happy coding !