This question already has answers here:
How to format numbers as currency string?
(65个答案)
3年前关闭。
我有一些辅助函数,将我的金钱价值格式化为磅和便士:
给我:
有什么方法可以将其准确地格式化为磅和便士吗?因此显示为
(65个答案)
3年前关闭。
我有一些辅助函数,将我的金钱价值格式化为磅和便士:
formatMoney: function(10496.470000000001){
return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "1,");
},
给我:
11,496.471,001,001,001
有什么方法可以将其准确地格式化为磅和便士吗?因此显示为
£11,496.471p
最佳答案
除了前面提到的toFixed方法调用之外,要替换的第二个参数应该是“ $ 1”,而不是“ 1”。另外,您转换成数字有些脆弱。这是我纠正这些问题的尝试:
function convert(value){
return "£"+((Number(value)||0).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"))+"p";
}
console.log(convert(10496.470000000001));
关于javascript - 将货币值格式化为磅和便士,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37679351/
10-11 11:11