Skip to content

formatNumber

Module Export

number |> (
locale: text,
decimals: number|nothing
) => text

Formats a number for display in the given BCP-47 locale, using the locale's grouping and decimal separators.

formatNumber renders a number the way a person expects to read it: grouped digits and the locale’s decimal separator. Without a decimals argument, the value’s own scale decides the precision — 1299.5 has one decimal and keeps exactly one.

Unresolvable locales fall back to invariant formatting, which is ungrouped and therefore never ambiguous.

Examples

Format with a locale

import { formatNumber } from 'text'
from 1299.5 formatNumber ('sv-SE')
// Returns:
// '1 299,5' (the group separator is a non-breaking space)

The locale decides the presentation

import { formatNumber } from 'text'
from 1299 formatNumber ('sv-SE')
// Returns:
// '1 299'

Override the decimals

import { formatNumber } from 'text'
from 1299.5 formatNumber ('sv-SE', 2)
// Returns:
// '1 299,50'

Unresolvable locales fall back to invariant

import { formatNumber } from 'text'
from 1299.5 formatNumber ('xx-BAD')
// Returns:
// '1299.5'

Notes

  • decimals requests are rounded to a whole number and clamped to 0–28 (the .NET decimal maximum scale)
  • The invariant fallback is ungrouped, matching the code-compatible form format produces
  • Several locales (sv-SE, fr-FR) use a non-breaking space as group separator, which can look like a plain space in output