Javascript:文字转数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11980087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:57:41  来源:igfitidea点击:

Javascript: Words to numbers

javascriptnumbersword

提问by JavaAndCSharp

How can I convert words to numbers in JavaScript?

如何在 JavaScript 中将单词转换为数字?

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

示例:“从现在起 19 天”将变为“从现在起 19 天”。

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

我可以使用 jQuery 或其他库 - 如果它不是 jQuery,希望是一个小库。

回答by JavaAndCSharp

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

这是我对@Greg Hewgill 的 Python 模块的 JavaScript 替换,减去代码底部的测试:

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.

我对 IE 中的 RegEx-ing 和 for-each 错误有点着迷,但它就是这样。从 Python 到 JavaScript 的相当不错的转换,如果我自己可以这么说,我不能。它需要一些工作——“一百二十”行不通——但现在已经足够了。当然,感谢@Greg Hewgill 用 Python 编写的原始版本。

回答by Finn Fitzsimons

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

这是链接:Words To Numbers

回答by Greg Hewgill

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

我为 Python 编写了一个小模块来执行此操作:https: //github.com/ghewgill/text2num/blob/master/text2num.py

It should be straightforward to convert that to Javascript.

将其转换为 Javascript 应该很简单。