简体   繁体   English

使用不带货币符号的 Intl.NumberFormat 进行货币格式化

[英]Currency formatting using Intl.NumberFormat without currency symbol

I am trying to use Intl as a default currency formatter and it is almost perfect.我正在尝试使用 Intl 作为默认货币格式化程序,它几乎是完美的。

Using the example from a Intl.NumberFormat() constructor :使用Intl.NumberFormat() 构造函数中的示例:

const number = 123456.789;

console.log(new Intl.NumberFormat('de-DE', { style: 'currency',
currency: 'EUR' }).format(number)); // expected output: "123.456,79 €"
 
// the Japanese yen doesn't use a minor unit 
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY'
}).format(number)); // expected output: "¥123,457"

This is almost perfect but I would actually like to drop the symbol from the output.这几乎是完美的,但我实际上想从 output 中删除符号。 So I would expect to see:所以我希望看到:

// expected output: "123.456,79"
// expected output: "123,457"

I find it bizarre that I spend over an hour looking for a solution and only found some sort of replace/trim usage.我发现我花了一个多小时寻找解决方案却只发现了某种替换/修剪用法,这很奇怪。

Why there is not an option to format the number with all the Intl power but only dropping the currency symbol???为什么没有选项可以使用所有 Intl 功能格式化数字,而只删除货币符号???

I hope I missed it, tbh.我希望我错过了,tbh。

One simple way to do achieve what you want is to use String#replace() to remove the currency from the string.实现您想要的一种简单方法是使用String#replace()从字符串中删除货币。 To make this easier, you can set currencyDisplay to "code" which will use the ISO currency code - the same one passed in to currency :为了更容易,您可以将currencyDisplay设置为"code" ,它将使用 ISO 货币代码 - 与传递给currency相同的代码:

 const number = 123456.789; console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR', currencyDisplay: "code" }) .format(number) .replace("EUR", "") .trim() ); // 123.456,79 // the Japanese yen doesn't use a minor unit console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY', currencyDisplay: "code" }) .format(number) .replace("JPY", "") .trim() ); // 123,457

This can be extracted into a function:这可以提取为一个函数:

 const number = 123456.789; console.log(format('de-DE', 'EUR', number)); // 123.456,79 console.log(format('ja-JP', 'JPY', number)); // 123,457 function format (locale, currency, number) { return new Intl.NumberFormat(locale, { style: 'currency', currency, currencyDisplay: "code" }) .format(number) .replace(currency, "") .trim(); }


An alternative that allows you more control is to use Intl.NumberFormat#formatToParts() which formats the number but gives you tokens that you can programmatically consume and manipulate.另一种允许您进行更多控制的替代方法是使用Intl.NumberFormat#formatToParts() ,它格式化数字但为您提供可以通过编程方式使用和操作的标记。 For example, using the method with locale = "de-DE" and currency = "EUR" you get the following output:例如,使用locale = "de-DE"currency = "EUR"您会得到以下输出:

[
  {
    "type": "integer",
    "value": "123"
  },
  {
    "type": "group",
    "value": "."
  },
  {
    "type": "integer",
    "value": "456"
  },
  {
    "type": "decimal",
    "value": ","
  },
  {
    "type": "fraction",
    "value": "79"
  },
  {
    "type": "literal",
    "value": " "
  },
  {
    "type": "currency",
    "value": "EUR"
  }
]

Which means that you can easily filter out "type": "currency" and then combine the rest into a string.这意味着您可以轻松过滤掉"type": "currency" ,然后将其余部分组合成一个字符串。 For example:例如:

 const number = 123456.789; console.log(format('de-DE', 'EUR', number)); // 123.456,79 console.log(format('ja-JP', 'JPY', number)); // 123,457 function format (locale, currency, number) { return new Intl.NumberFormat(locale, { style: 'currency', currency, currencyDisplay: "code" }) .formatToParts(number) .filter(x => x.type !== "currency") .map(x => x.value) .join("") .trim() }

If you only want the separators in between individual digits , just format as a number instead of using currency :如果您只想要单个数字之间的分隔符,只需格式化为数字而不是使用currency

const numberFormatter = new Intl.NumberFormat("en-US", {
  // Do not show fractions - front end should only handle whole numbers
  maximumFractionDigits: 0,
});

And then numberFormatter.format(asNumber);然后numberFormatter.format(asNumber); with whatever number you like.用你喜欢的任何number

For your example of 123456 , "de-DE" is 123.456 , "ja-JP" is 123,456对于您的123456示例, "de-DE"123.456"ja-JP"123,456

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM