简体   繁体   English

自动格式化印度货币号码

[英]Auto format Indian currency number

I am using following code to display currency in Indian numbering system:我使用以下代码在印度编号系统中显示货币:

<script type="text/javascript">
   function format(obj) {
      var formatted = obj.value.replace(/\D/g, '');
      obj.value = formatted.replace(/(\d+)(\d{2})/g,'$1.$2');
   }
</script>

<input type="number" onkeyup="format(this)">

Output I'm getting: 123456.78我得到的输出: 123456.78

Output I want is: 1,23,45,678.00我想要的输出是: 1,23,45,678.00

Please help!请帮忙!

You can use Intl.NumberFormat您可以使用Intl.NumberFormat

 const number = 12345678; console.log(new Intl.NumberFormat('en-IN').format(number));

If that should be a currency, you can pass that as a second argument如果那应该是一种货币,您可以将其作为第二个参数传递

 const number = 12345678; console.log(new Intl.NumberFormat('en-IN', {style: 'currency', currency: 'INR'}).format(number));

Or to force the two decimals:或强制两位小数:

 const number = 12345678; console.log(new Intl.NumberFormat('en-IN', {minimumFractionDigits: 2}).format(number));

Number.prototype.toLocaleString() will be of great help here. Number.prototype.toLocaleString()在这里会有很大帮助。

 function convertToIndianCurrency(x){ return x.toLocaleString('en-IN'); } console.log(convertToIndianCurrency(123456.789));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM