How to format international currencies in Javascript
Mark O'Neill
March 26, 2021
Show currencies and other numbers in the correct format by using the ECMAScript Internationalization API (Intl). This API also provides options for string comparison, number formatting, and date and time formatting.
javascript
export formatCurrency = (value, locale, currency) => {
return new Intl.NumberFormat(locale, {
style: "currency",
currency: currency,
maximumFractionDigits: 2
}).format.value(value)
}
const number = 123456.789
console.log(formatCurrency(number, 'de-DE', 'EUR'))
// Output: "123.456,79 €"
console.log(formatCurrency(number, 'ja-JP', 'JPY'))
// Output: "¥123,457"