Unit Formatting with Intl in JavaScript

Chris Coyier Chris Coyier on

Raymond Camden:

It’s been a little while since I last blogged about my favorite web platform feature, Intl.

Favorite?! Big words.

As someone who does technical writing, though, I get it. Let’s say you have a number, like 392, and you need to display that as megabytes. What is the absolutely correct way to show that in U.S. English? 392 MB? 392mb? 392mB? None of those, it’s actually 392MB in the “short” format and 392 megabytes in the “long” format. I like programmatically correct answers.

And better, you can change the locale and have it reformat as appropriate. Like it’s actually 392 MB for de-DE.

It's time to take your JavaScript to the next level

Frontend Masters logo

Frontend Masters is the best place on the web to really learn JavaScript. We have a complete learning path from the biggest and best teachers in JavaScript to help you make the most out of the web's biggest language.

7-Day Free Trial

One response to “Unit Formatting with Intl in JavaScript”

  1. Francesco says:

    This got be baffled for a moment, because the SI standard way to do it is with a space between the number and the unit (“392 MB”, not “392MB”.) It’s the “narrow” value that removes all spaces, here. If you use “short” there, it will show up with the correct space.

Leave a Reply

Your email address will not be published. Required fields are marked *

$839,000

Frontend Masters donates to open source projects through thanks.dev and Open Collective, as well as donates to non-profits like The Last Mile, Annie Canons, and Vets Who Code.