我需要印度货币格式,例如 100000 为 1,00,000,1234 为 1,234。
我试过这段代码,
function currencyFormat1(id) {
var x;
x = id.toString();
var lastThree = x.substring(x.length - 3);
var otherNumbers = x.substring(0, x.length - 3);
if (otherNumbers != '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
return res;
}
但它只适用于 java 脚本, 我需要这个作为核心 java 代码,我试图转换它但是
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
最佳答案
Java NumberFormat 会给你你想要的,但你也可以编写自己的方法:
public static String fmt(String s){
String formatted = "";
if(s.length() > 1){
formatted = s.substring(0,1);
s = s.substring(1);
}
while(s.length() > 3){
formatted += "," + s.substring(0,2);
s = s.substring(2);
}
return formatted + "," + s + ".00";
}
测试:
System.out.println(fmt("1234"));
System.out.println(fmt("100000"));
System.out.println(fmt("12345678"));
System.out.println(fmt("123456789"));
System.out.println(fmt("1234567898"));
输出:
1,234.00
1,00,000.00
1,23,45,678.00
1,23,45,67,89.00
1,23,45,67,898.00
关于javascript - 我需要印度货币格式,例如 100000 为 1,00,000,1234 为 1,234,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173466/