Convert integers into words.
Features and limitations:
- Languages: English and Japanese.
- Integer range: 0 → 253–1.
- While 64-bit floating-point numbers technically support numbers up to approximately 1.8 × 10308, there is a significant loss of precision. 253–1 is the largest integer with no loss of precision.
npm install int2wordsimport { int2en, int2ja } from 'int2words';
console.log(int2en(321)); // "three hundred twenty-one"
console.log(int2ja(321)); // "三百二十一"
console.log(int2en(-1)); // Invalid input: ""
/**
* Optionally throw an error.
*/
try {
int2en(-1, { throwError: true });
} catch (error) {
if (error instanceof CoercionError) {
console.error(error); // "CoercionError: Expected a positive integer."
}
throw error;
}const { int2en, int2jw } = require('int2words');To use in browsers directly and without bundlers.
- Save
int2words.min.jsto your project and add<script src="PATH/TO/int2words.min.js">in your HTML file. - Or add
<script src="https://unpkg.com/int2words@2.0.1/dist/legacy-iife/int2words.min.js">in your HTML file.
The library is now available globally through the int2words object.
console.log(int2words.int2en(321)); // "three hundred twenty-one"Update README, deps, etc.
Change name and modernize.
- Originally a modification from http://stackoverflow.com/questions/14766951/convert-digits-into-words-with-javascript.
This project is licensed under the GNU General Public License (GPL).