如何在网页上将数字转换为印度卢比格式,例如10,000.00 [英] How to get numbers to Indian rupees format in web page like 10,000.00

查看:120
本文介绍了如何在网页上将数字转换为印度卢比格式,例如10,000.00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入印度卢比格式附带的数字并且结果也以印度卢比格式粘贴在总输入框上时,如何使它变成数字? 请为此提供帮助.

How do I make it make when I enter the number it comes with Indian rupees format and the result also pastes on the total input box in the Indian rupees format? Please help with this.

<input onblur="findTotal()" type="text" name="qty" id="qty1"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty2"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty3"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty4"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty5"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty6"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty7"/><br>
<input onblur="findTotal()" type="text" name="qty" id="qty8"/><br>
<br><br>
Total : <input type="text" name="total" id="total"/>

这里是javascript

Here the javascript

<script type="text/javascript">
function findTotal(){
    var arr = document.getElementsByName('qty');
    var tot=0;
    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('total').value = tot;
}

</script>

推荐答案

您可以使用Intl.NumberFormat方法为您完成所有操作,我已经在下面创建了演示.

You can use the Intl.NumberFormat method to handle it all for you pretty much, I've created the demo below.

它与您现有的代码相似,不同之处在于它首先将值传递给数字格式器.

It's similar to your existing code, except it passes the values through the number formatter first.

var formatter = new Intl.NumberFormat('en-IN', {
  style: 'currency',
  currency: 'INR',
  minimumFractionDigits: 2,
});
//formatter.format(2500); /* $2,500.00 */
function findTotal(){
    var arr = document.getElementsByName('qty');
    var tot = 0;
    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('total').value = formatter.format(tot);
}

<input onkeyup="findTotal()" type="text" name="qty" id="qty1"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty2"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty3"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty4"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty5"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty6"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty7"/><br>
<input onkeyup="findTotal()" type="text" name="qty" id="qty8"/><br>
Total : <input type="text" name="total" id="total"/>

有关浏览器的支持,请参阅: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat#Browser_compatibility

For browser support, please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat#Browser_compatibility

这篇关于如何在网页上将数字转换为印度卢比格式,例如10,000.00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆