如何覆盖由 Intl.NumberFormat 使用的货币符号

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

How to override the currency symbol used by Intl.NumberFormat

问题

我正在使用 Intl.NumberFormat 来在网页上格式化货币,但我想使用特定的货币符号而不是定义的区域设置。

具体来说,我想在加拿大元的情况下显示 C$,而不是标准的 $,即使区域设置设置为 en-CAfr-CA

示例:

> Intl.NumberFormat('en-CA', { style: 'currency', currency: 'CAD' }).format(13999.59)
'$13,999.59'  // 预期结果 'C$13,999.59'

> Intl.NumberFormat('fr-CA', { style: 'currency', currency: 'CAD' }).format(13999.59)
'13 999,59 $' // 预期结果 '13 999,59 C$'

我想应该有一种方法可以 覆盖 货币符号,但我在文档中找不到如何做的方法。

那么,我如何在特定区域设置下覆盖货币符号呢?

英文:

I am using the Intl.NumberFormat to format currencies on a web page, but I want to use specific currency symbols instead of the locale defined ones.

To be more specific, I want to show C$ for Canadian dollars, instead of the standard $, even though the locale is set to en-CA or fr-CA.

Example:

> Intl.NumberFormat('en-CA', { style: 'currency', currency: 'CAD' }).format(13999.59)
'$13,999.59'  // expected 'C$13,999.59'

> Intl.NumberFormat('fr-CA', { style: 'currency', currency: 'CAD' }).format(13999.59)
'13 999,59 $' // expected '13 999,59 C$'

I suppose there is a way to override the currency symbol, but I couldn't find how in the docs.

So, how can I override the currency symbol for a specific locale?

答案1

得分: 2

是的。可以使用Intl.NumberFormat.prototype.formatToParts()方法实现。

在此查看文档

const enCA = Intl.NumberFormat('en-CA', { style: 'currency', currency: 'CAD' }).formatToParts(13999.59).map(({ type, value }) => {
    switch (type) {
      case "currency":
        return `C$`;
      default:
        return value;
    }
  }).reduce((str, pt) => str + pt);

const frCA = Intl.NumberFormat('fr-CA', { style: 'currency', currency: 'CAD' }).formatToParts(13999.59).map(({ type, value }) => {
    switch (type) {
      case "currency":
        return `C$`;
      default:
        return value;
    }
  }).reduce((str, pt) => str + pt);

console.log(enCA);
console.log(frCA);
英文:

Yes. It is possible with Intl.NumberFormat.prototype.formatToParts() method.

View the documentation here

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

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

const enCA = Intl.NumberFormat(&#39;en-CA&#39;, { style: &#39;currency&#39;, currency: &#39;CAD&#39; }).formatToParts(13999.59).map(({ type, value }) =&gt; {
    switch (type) {
      case &quot;currency&quot;:
        return `C$`;
      default:
        return value;
    }
  }).reduce((str, pt) =&gt; str + pt);
  
const frCA = Intl.NumberFormat(&#39;fr-CA&#39;, { style: &#39;currency&#39;, currency: &#39;CAD&#39; }).formatToParts(13999.59).map(({ type, value }) =&gt; {
    switch (type) {
      case &quot;currency&quot;:
        return `C$`;
      default:
        return value;
    }
  }).reduce((str, pt) =&gt; str + pt);

console.log(enCA);
console.log(frCA);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 21:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448736.html
匿名

发表评论

匿名网友

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

确定