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.

A-Tech
  • 806
  • 6
  • 22
JavaAndCSharp
  • 1,507
  • 3
  • 23
  • 47
  • 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 '12 at 03: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. – JavaAndCSharp Aug 16 '12 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 '21 at 04:40

3 Answers3

25

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
JavaAndCSharp
  • 1,507
  • 3
  • 23
  • 47
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

Finn Fitzsimons
  • 324
  • 3
  • 10
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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285