I am trying to configure my Intl.NumberFormat
instance to show a proper unit for large amounts but seems like it shows B
instead of G
for 1000000000, e.g. 1BB/s instead of 1GB/s.
Here is my instance:
const rateFormatter = Intl.NumberFormat('en', {
style: 'unit',
unit: 'byte-per-second',
unitDisplay: 'narrow',
notation: 'compact',
maximumFractionDigits: 2,
})
console.log(rateFormatter.format("1000000000"));
Why is it so? My browser is the latest Chrome on MacOS.
2
Answers
The issue seems to be with
Intl
itself.as your code in your question produces
1BB/s
if you change the
unitDisplay
to belong
instead, you will get"1B bytes per second"
in the console, likemeaning that the the issue is with the
format
functionTest 1
after some testing.
I’ve discovered that.
passing value of
1000000000
the output is"1B bytes per second"
(which you originaly passing in as per your question)100000000
the output is"100M bytes per second"
if we change the
unitDisplay
back tonarrow
Test 2
and if we run the same test again, we get
passing value of
1000000000
the output is"1BB/s"
(which you originaly passing in as per your question)100000000
the output is"100MB/s"
Solution
the issue is that the
B
refers tobillion
so the output is correct but it does not givebillion bytes per second
in the long unit display setting, so its not broken but rather, just badly implemented, with values so large it simple just uses 1B meaning 1 billionHere’s a workaround which checks the size of the number and picks an appropriate unit.