You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Intl.NumberFormat时遇Uncaught RangeError:无效货币代码,疑代码正确

解决Intl.NumberFormat的无效货币代码错误

你遇到的问题根源是:Intl.NumberFormat的currency选项需要传入ISO 4217标准的货币代码(如NGN),而不是货币符号(如\u20A6)。

\u20A6是尼日利亚奈拉的符号,但Intl不会识别这个符号作为货币标识,所以抛出了无效货币代码的错误,乱码后的â¦是该符号在错误编码环境下的显示结果。

修正后的代码如下:

export const toCommaAmount = (origNum) => {
    console.log("inside toCommaAmount")
    let newNum = origNum
    if (origNum !== null) {
        if (typeof origNum == 'string') {
            newNum = Number(origNum);
        }
        const options = {
            style: 'currency',
            currency: 'NGN' // 替换为ISO 4217货币代码
        }

        const intNum = new Intl.NumberFormat('en-US', options).format(newNum);
        return intNum.toString()
    }
}

如果需要强制显示₦符号而不是默认格式,可额外添加currencyDisplay: 'symbol'选项:

const options = {
    style: 'currency',
    currency: 'NGN',
    currencyDisplay: 'symbol'
}

内容的提问来源于stack exchange,提问作者MIike Eps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 01:20:01