Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Words to numbers [closed]

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.

like image 828
JavaAndCSharp Avatar asked Aug 16 '12 02:08

JavaAndCSharp


People also ask

How do I convert a string to a number in JavaScript?

How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.

How do I convert a string to a number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.

How can you convert the string of any base to integer in JavaScript?

To convert a string to an integer parseInt() function is used in javascript. parseInt() function returns Nan( not a number) when the string doesn't contain number. If a string with a number is sent then only that number will be returned as the output. This function won't accept spaces.


2 Answers

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.

like image 168
JavaAndCSharp Avatar answered Oct 11 '22 23:10

JavaAndCSharp


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

like image 28
Finn Fitzsimons Avatar answered Oct 11 '22 22:10

Finn Fitzsimons