英文:
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
英文:
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) => {
const currencyFormat = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0,
});
return item == null ? '' : 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
答案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
的默认值。
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) => {
const currencyFormat = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});
return item == null ? '' : 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:
<CcyNtry>
<CtryNm>UNITED STATES OF AMERICA (THE)</CtryNm>
<CcyNm>US Dollar</CcyNm>
<Ccy>USD</Ccy>
<CcyNbr>840</CcyNbr>
<CcyMnrUnts>2</CcyMnrUnts>
</CcyNtry>
The <CcyMnrUnts>2</CcyMnrUnts>
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.
mozilla article explaining implementation logic: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#currency_formatting
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论