javascript - 印度货币格式的数字到单词转换,按键时最多 16 位数字
问题描述
我试图在按键上以印度编号格式显示数字,最多 16 位数字,但 jquery 函数最多只能工作 10 位数字。这是我的代码片段
<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>
我通过谷歌搜索得到的用于转换的 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,则单词转换将是“一亿两千万十万”。
大于 10 位的数字返回空
如果数字是 10,20,10,00,000,则单词转换应为 102000 万
解决方案
我拆分数字以达到期望的结果,尽管它不能被称为准确的数字,但它解决了我的问题。
$('.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));
}
});
推荐阅读
- linq - 如何用逗号分隔的字符串或 int 映射到 IEnumerable
使用自动映射器 - swiftui - 在 SwiftUI 中的多个导航视图的情况下隐藏导航栏
- angular - 使用 Angular Bootstrap,我无法在下拉菜单中获取下拉项目
- reactjs - 如何在使用 React Player 更改 url 时停止播放加载的视频并加载新视频?
- meteor - 我无法在流星包中捕获用户媒体流
- java - 如何用 Maven 打包 Ant 项目?
- firebase - Firestore 模拟器显示“确定”
- javascript - React:你怎么知道一个组件的父组件是另一个组件
- wcf - IIS 托管 WCF 在调用之间共享数据
- amazon-web-services - AWS describe_containers 直接查询时返回空