简体   繁体   English

NumberFormat 返回错误的将数字转换为货币的格式

[英]NumberFormat returning wrong format for converting number to currency

Code:代码:

 let currency = 400023; console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(currency));

Expected: €400.023,00预计: 400.023,00 欧元

Actual: €400,023.00实际: 400,023.00 欧元

On the examples I found in the JS documentation, it shows dots being used as thousand separators and commas for cents, yet when I use it myself it doesn't work.在我在 JS 文档中找到的示例中,它显示点被用作千位分隔符和逗号用于美分,但是当我自己使用它时它不起作用。

As @RobG pointed out it's an issue with your JS implementation, which isn't supporting the Intl object properly.正如@RobG 指出的,这是您的 JS 实现的问题,它没有正确支持 Intl 对象。

All current browsers have full support for Intl.当前所有的浏览器都完全支持 Intl。

If you running your code on Node.js the situation is a little bit more complicated.如果您在 Node.js 上运行您的代码,情况会稍微复杂一些。 Before Node.js 13, there was only partial support for internationalization (ICU) including Intl.在 Node.js 13 之前,只有部分支持包括 Intl 在内的国际化 (ICU)。

Starting with Node.js 13 there is full ICU/internationalization support built in by default.从 Node.js 13 开始,默认情况下内置了完整的 ICU/国际化支持。 For details see:详情请见:

Essentialy, you can run this piece of code:本质上,您可以运行这段代码:

 console.log(new Intl.DateTimeFormat('es',{month:'long'}).format(new Date(9E8)));

Your output might be:您的输出可能是:

  • Error: No support for ICU - very old browser / Node.js错误:不支持 ICU - 非常旧的浏览器/Node.js
  • January: Partial support for ICU - old browser / Node.js < 13 1 月:部分支持 ICU - 旧浏览器 / Node.js < 13
  • enero: Full support for ICU - recent browser / Node.js >= 13 enero:完全支持 ICU - 最近的浏览器 / Node.js >= 13

In case your stuck with Node.js < 13, you can backfill full ICU support with https://www.npmjs.com/package/full-icu .如果您坚持使用 Node.js < 13,您可以使用https://www.npmjs.com/package/full-icu回填完整的 ICU 支持。

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

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