Words To Numbers
Convert words to numbers. Optionally fuzzy match the words to numbers.
npm install words-to-numbers
If the whole string passed is a number then it will return a Number
type otherwise it will return the original string with all instances of numbers replaced.
TODO: Add functionality for parsing mixed numbers and words. PRs welcome.
Basic Examples
import wordsToNumbers from 'words-to-numbers';
wordsToNumbers('one hundred');
wordsToNumbers('one hundred and five');
wordsToNumbers('one hundred and twenty five');
wordsToNumbers('four thousand and thirty');
wordsToNumbers('six million five thousand and two');
wordsToNumbers('a thousand one hundred and eleven');
wordsToNumbers('twenty thousand five hundred and sixty nine');
wordsToNumbers('five quintillion');
wordsToNumbers('one-hundred');
wordsToNumbers('one-hundred and five');
wordsToNumbers('one-hundred and twenty-five');
wordsToNumbers('four-thousand and thirty');
wordsToNumbers('six-million five-thousand and two');
wordsToNumbers('a thousand, one-hundred and eleven');
wordsToNumbers('twenty-thousand, five-hundred and sixty-nine');
Multiple numbers in a string
Returns a string with all instances replaced.
wordsToNumbers('there were twenty-thousand, five-hundred and sixty-nine X in the five quintillion Y'))
With Fuzzy Matching
Uses Jaro distance to find the best match for the number words. Don't rely on this being completely accurate...
import wordsToNumbers from 'words-to-numbers';
wordsToNumbers('won huntred', {fuzzy: true});
wordsToNumbers('too thousant and fiev', {fuzzy: true});
wordsToNumbers('tree millyon sefen hunderd and twinty sex', {fuzzy: true});
Decimal Points
import wordsToNumbers from 'words-to-numbers';
wordsToNumbers('ten point five');
wordsToNumbers('three point one four one five nine two six');
Ordinal Numbers
import wordsToNumbers from 'words-to-numbers';
wordsToNumbers('first');
wordsToNumbers('second');
wordsToNumbers('third');
wordsToNumbers('fourteenth');
wordsToNumbers('twenty fifth');
wordsToNumbers('thirty fourth');
wordsToNumbers('forty seventh');
wordsToNumbers('fifty third');
wordsToNumbers('sixtieth');
wordsToNumbers('seventy second');
wordsToNumbers('eighty ninth');
wordsToNumbers('ninety sixth');
wordsToNumbers('one hundred and eighth');
wordsToNumbers('one hundred and tenth');
wordsToNumbers('one hundred and ninety ninth');
Commonjs
const { wordsToNumbers } = require('words-to-numbers');
wordsToNumbers('one hundred');