CodePark/ questions / The Intl APIs
START PRACTICINGPRACTICE
Frontend question · Internationalization › The Intl APIs

What does this print?

console.log(new Intl.NumberFormat("de-DE").format(1234567.891));
console.log(
  new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" })
    .format(1234.5)
);
1.234.567,891 and ¥1,235

German uses a dot to group thousands and a comma for decimals, so the first line is 1.234.567,891. The yen has no minor unit in everyday use, so the currency formatter rounds to 0 decimals and prints ¥1,235.

"
¥1,234.50" is the tempting wrong answer. It assumes every currency has 2 decimals, but each currency has its own. JPY has 0 and the Kuwaiti dinar has 3. This is why you pass the currency code and let Intl choose, instead of calling toFixed(2).