我正在使用条纹结账,并将金额作为一个数字,如11999。如何将此数字显示为类似于$119.99的字符串
我试过new Intl.NumberFormat('en-IN', { currency: 'USD' }).format(11999)
但它是以$ 11,999的身份出现的
我也尝试过http://numeraljs.com,但是同样的问题。有谁有主意吗?
发布于 2019-05-29 15:50:59
首先,通过除以100 (一美元中的美分数量)将美分更改为美元。然后预置$。
const num = 11999;
const res = "$" + (num / 100);
console.log(res);
发布于 2019-05-29 15:53:20
你可以通过几种方式来实现。
第一种方法是将其解析为一个数字,然后将其除以100,但如果您的数字有两个以上的十进制数,那么这就有一个缺点。
let format = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
format.format(parseFloat(11999)/100);如果你的数字有更多的小数点,你可以这样做:
let numberOfDigits = 2;
format.format(parseFloat(11999)/numberOfDigits*10);https://stackoverflow.com/questions/56355404
复制相似问题