6

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?

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
nephews
  • 65
  • 1
  • 4
  • javascript interpreters represent millions of lines of code, if you find an error there you can open an error ticket to inform them – Mister Jojo Mar 06 '20 at 15:33
  • I understand. I was just wondering whether it's a bug or just that it's not supported after all. Can't find any information on it. – nephews Mar 06 '20 at 15:36
  • if the same js code gives 2 different results on 2 distinct interpreters, there is a good chance that this is an interpreter bug. – Mister Jojo Mar 06 '20 at 15:45
  • Unfortunately, we have given up on making a list of all unknown buggs, probably because there are too many! – Mister Jojo Mar 06 '20 at 16:13

1 Answers1

10

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.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • 5
    Thanks for clearing it up. I think MDN should clarify this in their docs, because when looking at the browser compatibility section it is kinda misleading. – nephews Mar 09 '20 at 09:50
  • 2
    [The proposal](https://github.com/tc39/proposal-unified-intl-numberformat) has been updated on March 23, 2020 with this status: "This proposal has been merged into ECMA-402 and is scheduled for the 2020 edition of the specification." – Matias Kinnunen Apr 19 '20 at 10:38
  • [See here](https://caniuse.com/#feat=mdn-javascript_builtins_intl_numberformat_numberformat_unit) for this feature's current browser compatibility. – Isaac Moore Aug 27 '20 at 15:16