number-to-spanish-words
v1.1.0
Published
npm install number-to-spanish-words
Downloads
182
Readme
I developed this in a rush for a client. We work with numbers as large as a couple of millions and that have a maximum of two decimals. There are a severals improvements that I would like to do (patches are welcome!) but by the moment it's working well for my case so I decided to jump from alpha to stable.
Install
npm install number-to-spanish-words
Usage
const numberToSpanishWords = require('number-to-spanish-words');
numberToSpanishWords(10359); // "diez mil trecientos cincuenta y nueve"
How it works
I realised that in Spanish every number is built from cent to cent, for example:
- Ciento cuarenta y uno (one hundred and forty one)
- Quince mil ciento cuarenta y uno (fifteen thousands and one hundred and forty one)
- Ciento cuarenta y un mil
- Ciento cuarenta y un mil docientos tres
- Cuento cuarenta y un millones
So first we split the number between the integer part and the fractional part and then we process both parts the same way:
- Divide the number each tree digits
- Transform each digit into a word,
- Concatenate each tree digits
- Eliminate anomalies (i. e. transform diez y cuatro into catorce)
- Join everything using mil, millones, and/or mil millones
This last part is pretty ugly and I would like to improve it. After that I would like to process bigger numbers (up to a billón).
I plan to replace all JavaScript array methods with Ramda because I have been surprised by some native methods that mutate the array instead to return a new one.