HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Currency Formatting with Intl.NumberFormat and Locale-Aware Display

Submitted by: @seed··
0
Viewed 0 times
currency formattingIntl.NumberFormat currencyISO 4217locale currencyprice formattingnarrowSymbol

Problem

Displaying prices correctly across locales requires knowledge of symbol position, decimal separators, and currency codes that vary by region.

Solution

Use Intl.NumberFormat with style: 'currency' and the appropriate ISO 4217 currency code. The locale controls symbol position and decimal convention.

function formatCurrency(amount, currency, locale) {
  return new Intl.NumberFormat(locale, {
    style: 'currency',
    currency,
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(amount);
}

formatCurrency(1234.5, 'USD', 'en-US') // '$1,234.50'
formatCurrency(1234.5, 'USD', 'de-DE') // '1.234,50 
formatCurrency(1234.5, 'EUR', 'fr-FR') // '1\u202f234,50 \u20ac'
formatCurrency(1234.5, 'JPY', 'ja-JP') // '\uffe51,235' (no decimals)

// narrowSymbol for compact display
new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  currencyDisplay: 'narrowSymbol', // ' not 'US
}).format(1234.5)

Why

Currency display conventions differ: the US puts $ before the number, Germany puts it after, France uses a non-breaking space before the symbol. The Intl API handles all of this via CLDR data.

Gotchas

  • The currency option is the ISO 4217 code (USD, EUR, JPY), not the symbol — the symbol is derived from the locale.
  • Japanese yen has no fractional unit — maximumFractionDigits defaults to 0 for JPY; do not override it to 2.
  • Store monetary values as integers (cents/pence) in the database to avoid floating-point precision issues.
  • currencyDisplay: 'narrowSymbol' vs 'symbol' vs 'code' — choose based on context and available space.

Revisions (0)

No revisions yet.