将数字转换为十万/千万系统中的单词

2022-01-17 00:00:00 numbers javascript

我正在编写一些将给定数字转换为单词的代码,这是我在谷歌搜索后得到的.但我认为这样一个简单的任务有点太长了.两个正则表达式和两个 for 循环,我想要更简单的.

I'm writing some code that converts a given number into words, here's what I have got after googling. But I think it's a bit too long for such a simple task. Two Regular Expressions and two for loops, I want something simpler.

我试图用尽可能少的代码行来实现这一点.到目前为止,这是我想出的:

I am trying to achieve this in as few lines of code as possible. here's what I've come up with so far:

有什么建议吗?

var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
 var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
 var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
 
function toWords(s) {
    s = s.toString();
    s = s.replace(/[, ]/g,'');
    if (s != parseFloat(s)) return 'not a number';
    var x = s.indexOf('.');
    if (x == -1)
        x = s.length;
    if (x > 15)
        return 'too big';
    var n = s.split(''); 
    var str = '';
    var sk = 0;
    for (var i=0;   i < x;  i++) {
        if ((x-i)%3==2) { 
            if (n[i] == '1') {
                str += tn[Number(n[i+1])] + ' ';
                i++;
                sk=1;
            } else if (n[i]!=0) {
                str += tw[n[i]-2] + ' ';
                sk=1;
            }
        } else if (n[i]!=0) { // 0235
            str += dg[n[i]] +' ';
            if ((x-i)%3==0) str += 'hundred ';
            sk=1;
        }
        if ((x-i)%3==1) {
            if (sk)
                str += th[(x-i-1)/3] + ' ';
            sk=0;
        }
    }
    
    if (x != s.length) {
        var y = s.length;
        str += 'point ';
        for (var i=x+1; i<y; i++)
            str += dg[n[i]] +' ';
    }
    return str.replace(/s+/g,' ');
}

另外,上面的代码转换成英文编号系统,例如 Million/Billion,我需要南亚编号系统,例如 Lakhs 和 Crores.

Also, the above code converts to the English numbering system like Million/Billion, I need the South Asian numbering system, like in Lakhs and Crores.

推荐答案

更新:看起来这比我想象的更有用.我刚刚在 npm 上发布了这个.https://www.npmjs.com/package/num-words

Update: Looks like this is more useful than I thought. I've just published this on npm. https://www.npmjs.com/package/num-words

这是一个较短的代码.一个正则表达式,没有循环.在南亚编号系统中随心所欲地转换

Here's a shorter code. with one RegEx and no loops. converts as you wanted, in south asian numbering system

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

function inWords (num) {
    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(d{2})(d{2})(d{2})(d{1})(d{2})$/);
    if (!n) return; var str = '';
    str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
    str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
    str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
    str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
    str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
    return str;
}

document.getElementById('number').onkeyup = function () {
    document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);
};

<span id="words"></span>
<input id="number" type="text" />

唯一的限制是,您最多可以转换 9 位数字,我认为这在大多数情况下已经足够了..

The only limitation is, you can convert maximum of 9 digits, which I think is more than sufficient in most cases..

相关文章