正确的JS货币格式,带逗号和小数 [英] Proper JS currency format with commas and decimals
问题描述
我在这里和其他地方浏览了无数线程超过2天,而我无法使其正常工作。
I have looked through countless threads here and elsewhere for over 2 days and I cannot get this to work properly.
我有一个完全按照我需要的方式工作的计算器,但是,我似乎无法完成最后一件事情。逗号分隔符,带小数点的千位。
I have a calculator working exactly the way I need it to, however, I can't seem to get one last thing complete. Comma separator by thousands with decimal.
我有小数位,但不能添加逗号。当我添加逗号时,我可以在计算中断或值显示为NaN之前使一个或两个字段起作用。
I have the decimal places, but can't add commas. When I do add commas, I can get one or two fields to work before the calculation breaks or the value is displayed as NaN.
这是工作页面: http://codepen.io/anon/pen/Fauzy
它当前使用的是:
function FormatAsMoney(mnt) {
mnt -= 0;
mnt = (Math.round(mnt*100))/100;
return (mnt == Math.floor(mnt)) ? mnt + '.00'
: ( (mnt*10 == Math.floor(mnt*10)) ?
mnt + '0' : mnt);
}
如果我尝试使用此功能:
If I try to use this :
function FormatAsMoney(x)
{
var money_value;
mnt = x.value;
mnt = mnt.replace(/\,/g,'');
mnt -= 0;
mnt = (Math.round(mnt*100))/100;
money_value = (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
if (isNaN(money_value))
{
money_value ="0.00";
}else{
money_value = CommaFormatted(money_value);
x.value = money_value.replace(".00", "");
}
}
这根本不起作用。
我使用了另一个测试:
function FormatAsMoney(str) {
return (str + "").replace(/\b(\d+)((\.\d+)*)\b/g, function(a, b, c) {
return (b.charAt(0) > 0 && !(c || ".").lastIndexOf(".") ? b.replace(/(\d)(?=(\d{3})+$)/g, "$1,") : b) + c;
});
}
其中第一个字段的格式为逗号,但丢失小数点它不会继续任何其他计算。
In which I get the comma formatting on the first field, but lose decimals and it does not continue any other calculation.
作为另一个示例,我创建了另一个函数来添加逗号,如:
As another example I created another function to add the commas like :
function addCommas(nStr){
nStr += '';
c = nStr.split(','); // Split the result on commas
nStr = c.join(''); // Make it back to a string without the commas
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
然后我像这样使用它:
document.Rate.RATE.value=addCommas(FormatAsMoney(dasum));
这似乎是我迄今为止获得的最好结果,但是在线(163)例如, function dosum()
取决于许多if语句,它将再次中断。我似乎无法使它适用于值将为数千的所有适用字段。
Which seems to be the best outcome I have had yet, however on line (163) where, for example, function dosum()
depends on many if statements, It breaks the again. I can't seem to get it to work for all applicaable fields where the value would be in thousands.
我需要能够在中输入保险金额最高的2000万美元 20000000(例如,因为它将填充几乎所有用逗号分隔的值并用小数位表示的字段)
I need to be able to enter the "Insured Amount" at the top 20 million dollars "20000000" (as an example because it will populate almost all possible fields that will have a comma separated values with a decimal place)
任何人都可以结束我的痛苦?谢谢您的帮助。
Can anyone end my misery? Thanks for any help.
推荐答案
尝试了许多不同的解决方案之后,(对我来说)这似乎是最好的方法:
After trying many different solutions, this seems (to me) the best way:
(1234568).toLocaleString("en-US", {style: "decimal", minimumFractionDigits: 2});
或
var opts = '{style: "decimal", currency: "USD", minimumFractionDigits: 2}';
(1234568).toLocaleString("en-US", opts);
或
(1234568).toLocaleString("en-US", {style: "decimal", minimumFractionDigits: 0});
我喜欢 NickG在2013年中发表的评论:同意,尚未在所有浏览器中完全支持它,但它仍然是解决方案。 (可以说是最有效的解决方案,因为它与不受支持的浏览器向前兼容,并且是Javascript api的文档功能。)
I liked NickG's comment from mid-2013: "Agreed, it's not fully supported across all browsers (yet), but it's still a solution. (And arguably the most valid solution, as its forward compatible with the non-supported browsers, and it's a documented feature of the Javascript api.)"
来源:
< a href = https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript#answer-17663871>如何打印数字像逗号一样用逗号JavaScript中的ds分隔符
这篇关于正确的JS货币格式,带逗号和小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!