Glossary / Locale-aware numbers

Locale-aware numbers

Locale-aware number formatting presents numeric values using a user’s conventions for decimal and grouping separators, digits, percent, currency, signs, and units. The same value can appear as 1,234.5, 1.234,5, or use non-Latin digits depending on locale. Store numbers as numeric values and format them for display with locale data; parse user input according to a declared locale rather than stripping punctuation heuristically.

What to know

Testing and pitfalls

Test decimals, large values, zero, negative amounts, percentages, currencies with different minor units, and locales with alternate digits or separators. Validate user input without silently misreading 1,234 as 1.234 or 1234. Keep raw value and display string separate. Do not use locale-formatted strings for sorting, calculations, APIs, or database keys.

Interview questions

  1. Why not format with toFixed? It lacks locale conventions and can expose binary floating-point rounding surprises.
  2. What should represent money? A value with currency code and precision/rounding rules; use a decimal-safe representation where exact arithmetic is needed.
  3. How do you parse localized numeric input? Define accepted locale and grammar, normalize validated digits/separators carefully, and reject ambiguous or malformed forms rather than guessing.
  4. Why can $ be insufficient? Multiple currencies use the symbol; in cross-border contexts display the code or localized currency name.
  5. What is a common percent bug? Intl percent formatting expects a fractional value (for example 0.15 to display 15%), so passing 15 can display 1500%.

References