10

How can I convert words to numbers in JavaScript?

Example: "Nineteen days from now" would become "19 days from now".

I'm fine with using jQuery or another library - hopefully a smallish one if it's not jQuery.

3
  • What about punctuation, e.g., "twenty-two" versus "twenty two". What about "I like cookies: I ate one-hundred and twenty were left over for later." (Which should obviously be "...I ate 100 and 20 were left...", not "...I ate 120 were left...".) What I'm saying is it could be difficult if not impossible to identify exactly which parts of the string form discrete numbers. Unless there is a very restricted range of numbers to identify, e.g., just zero through twenty or something like that...
    – nnnnnn
    Aug 16, 2012 at 3:39
  • Nope, no homework or job interviews. It's summer! That eliminates a homework assignment. Plus, I was tired, and couldn't find anything else that did the job, and got stuck when trying to write my own (it broke half the time, part of which I suspect is a Chrome bug) Plus, there's @nnnnnn's comment and a few other issues inherent in parsing text. Aug 16, 2012 at 13:04
  • You can use an object in a function where you pass in the string as an argument and return the corresponding value. For example function function word(s) { let obj = { 'zero': 0 }; return obj[s] }
    – nCardot
    Sep 14, 2021 at 4:40

3 Answers 3

26

Here's my JavaScript replacement of @Greg Hewgill's Python module, minus the testing from the bottom of the code:

var Small = {
    'zero': 0,
    'one': 1,
    'two': 2,
    'three': 3,
    'four': 4,
    'five': 5,
    'six': 6,
    'seven': 7,
    'eight': 8,
    'nine': 9,
    'ten': 10,
    'eleven': 11,
    'twelve': 12,
    'thirteen': 13,
    'fourteen': 14,
    'fifteen': 15,
    'sixteen': 16,
    'seventeen': 17,
    'eighteen': 18,
    'nineteen': 19,
    'twenty': 20,
    'thirty': 30,
    'forty': 40,
    'fifty': 50,
    'sixty': 60,
    'seventy': 70,
    'eighty': 80,
    'ninety': 90
};

var Magnitude = {
    'thousand':     1000,
    'million':      1000000,
    'billion':      1000000000,
    'trillion':     1000000000000,
    'quadrillion':  1000000000000000,
    'quintillion':  1000000000000000000,
    'sextillion':   1000000000000000000000,
    'septillion':   1000000000000000000000000,
    'octillion':    1000000000000000000000000000,
    'nonillion':    1000000000000000000000000000000,
    'decillion':    1000000000000000000000000000000000,
};

var a, n, g;

function text2num(s) {
    a = s.toString().split(/[\s-]+/);
    n = 0;
    g = 0;
    a.forEach(feach);
    return n + g;
}

function feach(w) {
    var x = Small[w];
    if (x != null) {
        g = g + x;
    }
    else if (w == "hundred") {
        g = g * 100;
    }
    else {
        x = Magnitude[w];
        if (x != null) {
            n = n + g * x
            g = 0;
        }
        else { 
            alert("Unknown number: "+w); 
        }
    }
}

I got hung up a bit on the RegEx-ing and the for-each bug in IE, but here it is. A fairly decent conversion from Python to JavaScript, if I can say so myself, which I can't. It needs some work - "One hundred and twenty" won't work - but it's good enough for now. Of course, thanks to @Greg Hewgill for the original, written in Python.

5
  • I just noticed that "sextillion" was spelled incorrectly in my code. I've changed it on github. Aug 19, 2012 at 20:05
  • 2
    if(w == "and"){return} May 10, 2014 at 19:38
  • This doesn't work correctly if you have text with multiple numbers (e.g. "Nineteen days to my desired million"); Dec 3, 2014 at 17:08
  • doesn't work if the number is "million" instead of "one million" Jan 26, 2018 at 11:38
  • However what I have done is to add 'a': 1, to the Small list :-) Thanks for your effort. Jan 26, 2018 at 12:24
10

This is an (really) old thread but if somebody else stumbles across I have made a node module inspired by the answers above that does all the above which can also:

  • handle dashes and commas
  • match multiple numbers in a string
  • fuzzy match the number words
  • interpret decimal places

Here's the link: Words To Numbers

3
  • I cannot install this package on my machine... Oct 12, 2017 at 6:24
  • it works I assure you - can you create a ticket here? github.com/finnfiddle/words-to-numbers/issues Oct 13, 2017 at 13:28
  • 1
    This looks awesome! Does it include a way to get the number values, rather than just converting the string? Something like wordsToNumbers(“boil ten eggs for five minutes”).getValues() // returns [10, 5] Apr 19, 2020 at 17:50
5

I wrote a little module for Python that does this: https://github.com/ghewgill/text2num/blob/master/text2num.py

It should be straightforward to convert that to Javascript.

1
  • 1
    Awesome, thanks. I'll post something here as an answer when I've converted it, which shouldn't be long. Aug 16, 2012 at 14:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.