Why doesn't Intl.NumberFormat work with units in Safari and Firefox?

笑着哭i 提交于 2020-06-16 05:47:08

问题


I'm trying to use the example given here concerning formatting a number.

console.log((16).toLocaleString('en-GB', {
    style: "unit",
    unit: "liter",
    unitDisplay: "long"
}));

I tried it in chrome and it works great. However, in Safari as well as Firefox I get the following errors respectively Error: style must be either "decimal", "percent", or "currency" and Error: invalid value "unit" for option style.

From the docs I figured it should work on all browsers after looking at the browser compatibility. I tried looking for answers, but I can't find anything regarding this issue. Does anyone know why this is or where I could probably find more info?


回答1:


The problem comes from the value unit of the style field.

According to ECMA-402, 6th edition, June 2019 ECMAScript® 2019 Internationalization API Specification:

The value of this field must be a record, which must have fields with the names of the three number format styles: "decimal", "percent", and "currency".

According to the Draft ECMA-402 / February 27, 2020 ECMAScript® 2020 Internationalization API Specification:

The value of this field must be a Record, which must have fields with the names of the four number format styles: "decimal", "percent", "currency", and "unit".

Firefox and Safari are implementing the 6th edition of the ECMA-402 specification and Chrome is implementing the Draft version of this same specification. The draft specification can change at any time and there is no formal guarantees that this new unit value will be included in the 7th edition. If you want to be cross-browser compatible and future-proof, you should stick on the 6th edition and wait for the release of the 7th edition before using these new features.

If you want details you can read the proposition for this new feature.



来源:https://stackoverflow.com/questions/60566942/why-doesnt-intl-numberformat-work-with-units-in-safari-and-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!