Intl.NumberFormat: “RangeError maximumFractionDigits value is out of range”

huangapple go评论91阅读模式
英文:

Intl.NumberFormat : "RangeError maximumFractionDigits value is out of range"

问题

I am receiving this error with the following code in React Typescript. What input parameter could cause this error and how can I resolve it? It seems to have an issue with Intl.NumberFormat parameters.

尝试找到导致此错误的输入参数,以在我们的Rollbar错误日志应用程序中解决它。尚未显示输入参数是什么,它偶尔发生。

> 错误:RangeError的maximumFractionDigits值超出范围。

<!-- 开始代码片段:js 隐藏:false 控制台:true babel:true -->

<!-- 语言:lang-js -->

const formatCurrency = (item: number) => {
const currencyFormat = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0,
});
return item == null ? '' : currencyFormat.format(item);
}

<!-- 结束代码片段 -->

注意:似乎在Mozilla Firefox中发生,而在Chrome中没有。不确定对于这种情况是否有影响。

Mozilla/5.0(Macintosh;Intel Mac OS X 10_10_5)AppleWebKit/537.36(KHTML,像Gecko一样)Chrome/87.0.4280.88 Safari/537.36

https://stackoverflow.com/questions/31864756/what-are-the-differences-between-firefox-and-chrome-in-javascript

英文:

I am receiving this error with the following code in React Typescript. What input parameter could cause this error and how can I resolve it? It seems to have issue with Intl.NumberFormat parameters.

Trying to find input parameter causing this in our Rollbar error logging application. Its not showing yet, what input parameter was, it happens rarely.

> Error: RangeError maximumFractionDigits value is out of range.

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

const formatCurrency = (item: number) =&gt; {
  const currencyFormat = new Intl.NumberFormat(&#39;en-US&#39;, {
    style: &#39;currency&#39;,
    currency: &#39;USD&#39;,
    maximumFractionDigits: 0,
  });
  return item == null ? &#39;&#39; : currencyFormat.format(item);
}

<!-- end snippet -->

Note: seems to occur in Mozilla Firefox, not Chrome. Not sure if it makes a difference for this case

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36

https://stackoverflow.com/questions/31864756/what-are-the-differences-between-firefox-and-chrome-in-javascript

答案1

得分: 1

你需要使用以下选项设置:

const formatCurrency = (item: number) => {
  const currencyFormat = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  });
  return item == null ? '' : currencyFormat.format(item);
}

请注意增加了 minimumFractionDigits: 0。在 XML 表示的 USD 货币逻辑中,<CcyMnrUnts>2</CcyMnrUnts> 部分指定了 minimumFractionDigits 的默认值。

XML 规范:https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml

Mozilla 文章解释了实现逻辑:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#currency_formatting

英文:

You need to use the following option set:

const formatCurrency = (item: number) =&gt; {
  const currencyFormat = new Intl.NumberFormat(&#39;en-US&#39;, {
    style: &#39;currency&#39;,
    currency: &#39;USD&#39;,
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  });
  return item == null ? &#39;&#39; : currencyFormat.format(item);
}

Note the addition of minimumFractionDigits: 0.

There is a large body of business logic that determines what default values are, according to various customs. The XML representing that logic for USD currency looks like this:

&lt;CcyNtry&gt;
  &lt;CtryNm&gt;UNITED STATES OF AMERICA (THE)&lt;/CtryNm&gt;
  &lt;CcyNm&gt;US Dollar&lt;/CcyNm&gt;
  &lt;Ccy&gt;USD&lt;/Ccy&gt;
  &lt;CcyNbr&gt;840&lt;/CcyNbr&gt;
  &lt;CcyMnrUnts&gt;2&lt;/CcyMnrUnts&gt;
&lt;/CcyNtry&gt;

The &lt;CcyMnrUnts&gt;2&lt;/CcyMnrUnts&gt; part is what specifies the default value for minimumFractionDigits

An Intl.Numberformat object of style "currency" and currency "USD" has an implied default minimumFractionDigits of 0, and you cannot have a maxiumum that is lower than the minimum.

xml spec: https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml

mozilla article explaining implementation logic: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#currency_formatting

huangapple
  • 本文由 发表于 2023年5月23日 01:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定