我正在使用 JavaScript Intl 对象,我想将数字(例如 150)格式化为看起来像“£150”。对于 Chrome,直到更新到版本 59,此代码才能完美运行:
var GBPFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'GBP',
maximumFractionDigits: 0
});
但现在它说“maximumFractionDigits”不能为 0,在阅读 https://developer.mozilla.org/ 之后我发现对于 GBP maximumFractionDigits 不能小于 2。
一切都很好,但我仍然需要“£150”而不是“£150.00”。任何想法我仍然可以使用 Intl 对象并使其看起来像我需要的那样。
附言我知道我可以使我自己的功能具有相同的剂量,但考虑到我不仅有英镑而且有更多的货币,我更愿意坚持使用现成的解决方案。
最佳答案
您可以根据需要将 maximumFractionDigits
选项设置为 0
,但该值需要高于 minimumFractionDigits
。因为此货币的 minimumFractionDigits
的默认值高于 0
,所以您需要手动设置这两个值以获得您想要的:
var GBPFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'GBP',
minimumFractionDigits: 0,
maximumFractionDigits: 0
});
console.log(GBPFormatter.format(150)); // Should output "£150"
关于javascript - 货币的 JS Intl.NumberFormat 不能有 maximumFractionDigits 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43940495/