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.


Reply to Stefan