skip to Main Content

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


  1. The issue seems to be with Intl itself.

    as your code in your question produces 1BB/s

    if you change the unitDisplay to be long instead, you will get "1B bytes per second" in the console, like

    const rateFormatter = Intl.NumberFormat('en', {
        style: 'unit',
        unit: 'byte-per-second',
        unitDisplay: 'long',
        notation: 'compact',
        maximumFractionDigits: 2,
      })
      
     console.log(rateFormatter.format("1000000000"));

    meaning that the the issue is with the format function

    Test 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 to narrow

    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 to billion so the output is correct but it does not give billion 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 billion

    Login or Signup to reply.
  2. Here’s a workaround which checks the size of the number and picks an appropriate unit.

    const bandwidth = (bytespersec) => {
       let units = "byte-per-second";
       let divisor = 1;
       if (bytespersec >= 1e9) {
         units = "gigabyte-per-second";
         divisor = 1e9;
       } else if (bytespersec >= 1e6) {
         units = "megabyte-per-second";
         divisor = 1e6;
       } else if (bytespersec >= 1e3) {
         units = "kilobyte-per-second";
         divisor = 1e3;
       }
       // etc, add more if needed.  Be sure to put the largest unit first in the above if (or re-do the logic)
       
       let f = Intl.NumberFormat('en', {
          style: 'unit',
          unit: units,
          unitDisplay: 'short',
          notation: 'compact',
          maximumFractionDigits: 2,
       });
       
       return f.format(bytespersec / divisor);
     };
     
     console.log(bandwidth(1));
     console.log(bandwidth(1025));
     console.log(bandwidth(1234567));
     console.log(bandwidth(234567890));
     console.log(bandwidth(2000000000));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search