【问题标题】:Number to word conversion in Indian currency format up to 16 digit number on key press印度货币格式的数字到单词转换,按键时最多 16 位数字
【发布时间】:2020-09-03 19:32:47
【问题描述】:

我试图在按键上以Indian numbering format 中的单词显示数字,最多 16 位数字,但 jquery 函数最多只能工作 10 位数字。这是我的代码 sn-p

<div class="form-group mb-3 customtooltip col-6">
        <p>
           <input type="text" id="text" class="mb-3 form-control allow_decimal" />            
            <b><span class="spnWord"></span></b>
        </p>
</div> 

我通过google搜索得到的用于转换的JS函数

$('.allow_decimal').keyup(function (event) {

      $(this).val(function (index, value) {
            return value.replace(/\D/g, '').replace(/(\d)(?=(\d\d)+\d$)/g, "$1,"); // to add commas 
       });

       $(this).closest('.form-group').find('.spnWord').text(price_in_words(this.value));
});

function price_in_words(price1) {
            console.log(price1);
            // Just remove commas and periods - regex can do any chars
            price = price1.replace(/([,€])+/g, '');
            var sglDigit = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
                dblDigit = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"],
                tensPlace = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"],
                handle_tens = function (dgt, prevDgt) {
                    return 0 == dgt ? "" : " " + (1 == dgt ? dblDigit[prevDgt] : tensPlace[dgt])
                },
                handle_utlc = function (dgt, nxtDgt, denom) {
                    return (0 != dgt && 1 != nxtDgt ? " " + sglDigit[dgt] : "") + (0 != nxtDgt || dgt > 0 ? " " + denom : "")
                };
            var str = "",
                digitIdx = 0,
                digit = 0,
                nxtDigit = 0,
                words = [];
            if (price += "", isNaN(parseInt(price))) str = "";
            else if (parseInt(price) > 0 && price.length <= 10) {
                for (digitIdx = price.length - 1; digitIdx >= 0; digitIdx--) switch (digit = price[digitIdx] - 0, nxtDigit = digitIdx > 0 ? price[digitIdx - 1] - 0 : 0, price.length - digitIdx - 1) {
                    case 0:
                        words.push(handle_utlc(digit, nxtDigit, ""));
                        break;
                    case 1:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 2:
                        words.push(0 != digit ? " " + sglDigit[digit] + " Hundred" + (0 != price[digitIdx + 1] && 0 != price[digitIdx + 2] ? " and" : "") : "");
                        break;
                    case 3:
                        words.push(handle_utlc(digit, nxtDigit, "Thousand"));
                        break;
                    case 4:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 5:
                        words.push(handle_utlc(digit, nxtDigit, "Lakh"));
                        break;
                    case 6:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 7:
                        words.push(handle_utlc(digit, nxtDigit, "Crore"));
                        break;
                    case 8:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 9:
                        words.push(0 != digit ? " " + sglDigit[digit] + " Hundred" + (0 != price[digitIdx + 1] || 0 != price[digitIdx + 2] ? " and" : " Crore") : "")
                }
                str = words.reverse().join("")
            }    
            console.log(str);
            return str

        }

当前的 Jquery 最多可以处理 10 位数字,例如

如果数字是 1,20,10,00,000,则单词转换将是“100 亿 100 万”。

大于10位的数返回空

如果数字是 10,20,10,00,000,则单词转换应该是 102000000000000

【问题讨论】:

    标签: javascript jquery asp.net-mvc


    【解决方案1】:

    我拆分数字以达到期望的结果,尽管它不能被称为准确的数字,但它解决了我的问题。

    $('.allow_decimal').keyup(function (event) {
                $(this).val(function (index, value) {
    
                    return value
                        .replace(/\D/g, '')
                        .replace(/(\d)(?=(\d\d)+\d$)/g, "$1,")
                        ;
                });
                var money = this.value;            
                var length = money.length;
                if (length < 15) {
                    $(this).closest('.form-group').find('.spnWord').text(price_in_words(this.value));
                } else {
                    var moneys = money.replace(new RegExp(',', 'g'), '');                             
                    var lacvalue = moneys.substring(moneys.length - 7, moneys.length);
                    var croreValue = moneys.substr(0, moneys.length - 7);
                    $(this).closest('.form-group').find('.spnWord').text(price_in_words(croreValue) + ' Crore ' + price_in_words(lacvalue));                
                }
            });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2014-02-18
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多