1

I am trying to use NumberFormat in NodeJS. It is working perfect for style "currency" but for style "unit" it is throwing the error: RangeError: Value unit out of range for numberformat options property style

Here is my code:

Intl.NumberFormat("en-US", {
    style: "unit",
    unit: "kilometer-per-hour",
  }).format(71);

This is only happening in NodeJS. I tried the same code in browser console and it worked perfectly.

Sid
  • 115
  • 1
  • 10

1 Answers1

0

You need to use polyfill to support in all browser.

const formatter = new Intl.NumberFormat('en', {
  style: 'unit',
  unit: 'kilometer-per-hour',
});

console.log(formatter.format(7024));
<html>
<head></head>
<body>

<!-- Polyfill Intl.NumberFormat, its dependencies & `en` locale data -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=Intl.NumberFormat,Intl.NumberFormat.~locale.en"></script>
</body>
</html>
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22