You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

印度货币格式数字转单词及输入框自动添加卢比符号实现

解决输入框自动添加印度卢比符号的方案

关键修改点

  • 在格式化数字的同时,自动在开头添加印度卢比符号₹,空输入时不显示符号
  • 调整数字转单词函数的正则规则,把卢比符号纳入清理范围
  • 处理用户手动删除符号的情况,确保符号始终保留在输入框开头

修改后的完整代码

$('.allow_decimal').keyup(function (event) {
    $(this).val(function (index, value) {
        // 先清理所有非数字字符(包括已存在的卢比符号)
        const cleanedValue = value.replace(/\D/g, '');
        // 有数字则添加卢比符号+印度格式分隔符,无数字则返回空
        return cleanedValue ? `₹${cleanedValue.replace(/(\d)(?=(\d\d)+\d$)/g, "$1,")}` : '';
    });

    // 更新对应的英文单词显示
    $(this).closest('.form-group').find('.spnWord').text(price_in_words(this.value));
});

function price_in_words(price1) {
    console.log(price1);
    // 移除逗号、欧元符号和印度卢比符号,只保留纯数字
    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
}

功能说明

  1. 输入框实时处理:每次按键后先清除所有非数字内容,再根据是否有数字决定是否添加卢比符号,确保符号始终在输入框最左侧,用户手动删除也会自动恢复
  2. 数字转单词适配:更新正则规则,把卢比符号加入清理列表,避免符号干扰数字解析逻辑,保证单词转换功能正常运行

内容的提问来源于stack exchange,提问作者AIYUB KHAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 07:52:24