最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS Intl.NumberFormat for currency cant have maximumFractionDigits 0 - Stack Overflow

programmeradmin3浏览0评论

I am using the JavaScript Intl object, and I want to format a number (150 for example) to look like "£150". For Chrome untill the update to version 59 this code worked perfect:

var GBPFormatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'GBP',
      maximumFractionDigits: 0
  });

I am using the JavaScript Intl object, and I want to format a number (150 for example) to look like "£150". For Chrome untill the update to version 59 this code worked perfect:

var GBPFormatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'GBP',
      maximumFractionDigits: 0
  });

but now it says that the "maximumFractionDigits" can not be 0, and after reading in https://developer.mozilla.org/ i found that for GBP maximumFractionDigits can not be less than 2.

All well and good, but i still need to have "£150" not "£150.00". Any ideas how i can still use the Intl object and make it look the way i need.

P.S. I know that i can make my own function that dose the same, but considering that i do not have only GBP but a bit more currencyes, I prefer to stick with a ready solution.

Share Improve this question asked May 12, 2017 at 14:42 Nikola.grancharovNikola.grancharov 6726 silver badges14 bronze badges 1
  • How about formatting the result of the formatter using a wrapper function - removing the fraction digits? – jjjjjjjjjjjjjjjjjjjj Commented May 12, 2017 at 15:06
Add a comment  | 

1 Answer 1

Reset to default 19

You can set the maximumFractionDigits option to 0 like you want, but the value needs to be higher than minimumFractionDigits. Because the default value for minimumFractionDigits is higher than 0 for this currency, you'll need to set both of them manually to get what you want:

var GBPFormatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'GBP',
  minimumFractionDigits: 0,
  maximumFractionDigits: 0
});

console.log(GBPFormatter.format(150)); // Should output "£150"

发布评论

评论列表(0)

  1. 暂无评论