简体   繁体   中英

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

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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