intermediate

Number formatting

Format numbers, currency, percentages, units, separators, and rounding rules according to locale and business meaning.

Number formatting covers digits grouping, decimal separators, currencies, percentages, and units per locale. `Intl.NumberFormat` is the browser/Node baseline; accounting rules (minor units, rounding) come from business requirements.

					new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(1234.5);
// "1.234,50 €"

new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 1 }).format(0.847);
// "84.7%"
				

| Concern | Note | |---------|------| | Currency | Use ISO 4217 code; watch zero-decimal currencies (JPY) | | Rounding | Bankers vs away-from-zero—document policy | | Compact notation | `notation: 'compact'` for dashboards | | Parsing user input | Normalize using locale-aware parser, not `parseFloat` alone |

Never hard-code `,` or `.` as thousands/decimal separators in shared components.

On interviews: storing money as integer minor units; displaying crypto vs fiat; SSR formatting consistency; CLDR updates across environments.

Common pitfalls: floating-point money; mixing currency symbol position; formatting server numbers with wrong locale header; stripping separators before parse incorrectly.

The trade-off is simple string templates versus correct financial and analytics display worldwide.

Checklist:

  • Format with explicit locale + currency/options.
  • Store money in minor units or decimal type with policy.
  • Parse input with locale-aware utilities.
  • Test de-DE, en-US, and one RTL market.