Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

Today I learned that Intl allows you to format numbers with currencies or units!

console.log(
  new Intl.NumberFormat("de-DE", { 
    style: "currency", 
    currency: "EUR" 
  }).format(123456789),
); // "123.456.789,00 €"

console.log(
  new Intl.NumberFormat("ja-JP", { 
    style: "currency", 
    currency: "JPY" 
  }).format(123456789),
); // "¥123,456,789"

console.log(
  new Intl.NumberFormat("pt-PT", {
    style: "unit",
    unit: "kilometer-per-hour",
  }).format(50),
); // "50 km/h"

console.log(
  new Intl.NumberFormat('fr-FR', {
    style: 'unit',
    unit: 'kilobyte',
    unitDisplay: 'long',
  }).format(123456)
); // "123 456 kilooctets"

Did you notice how the Japanese Yen symbol (¥) moved in front of the numbers? Or that, apparently, French folks translate the unit kilobyte? Fun!

Whenever you put currency/unit logic into userland JavaScript, don't. It's all baked into the language these days.

Tip: here's the list of supported units.

If you enjoyed this article...

Join 6.5k readers and learn something new every week with Web Weekly.

Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles