Skip to content

formatNumber

formatNumber(value, format?, defaultLocale?): string

Defined in: packages/core/src/i18n/number-format.ts:122

Format a number with MapLibre number-format options.

ParameterTypeDescription
valuenumberthe number.
formatNumberFormat{ locale, currency, unit, min-fraction-digits, max-fraction-digits }, or an older ",.2f" string.
defaultLocale?stringthe locale when format names none (usually the page’s language, elementLanguage); undefined means the browser’s.

string

the formatted text. An option Intl rejects (an unknown currency or unit, a malformed locale) is dropped with one console warning, never thrown.

Locale tags are read with normalizeLocale (en_US → en-US). A locale or option Intl rejects never loses more than it must: the options are tried under the default locale, then the browser’s, then grouping and fraction digits alone — the bare String(value) is the last resort.

formatNumber(1234567.891, { locale: "de-DE", "max-fraction-digits": 1 }); // "1.234.567,9"
formatNumber(12.5, { currency: "EUR" }, "fr"); // "12,50 €"
formatNumber(10, {}, "en") + "–" + formatNumber(25, {}, "en"); // "10–25"