getNumberFormatter() function

Returns a number formatter function for the given locales and number skeleton

Signature:

export declare function getNumberFormatter(locales: string | string[], skeleton: string | Skeleton, currency?: string | null, onError?: (err: NumberFormatError) => void): (value: number) => string;

Parameters

Parameter Type Description
locales string | string[] One or more valid BCP 47 language tags, e.g. fr or en-CA
skeleton string | Skeleton An ICU NumberFormatter pattern or ::-prefixed skeleton string, or a parsed Skeleton structure
currency string | null (Optional) If skeleton is a pattern string that includes ยค tokens, their skeleton representation requires a three-letter currency code.
onError (err: NumberFormatError) => void (Optional) If defined, will be called separately for each encountered parsing error and unsupported feature.

Returns:

(value: number) => string

Remarks

Uses Intl.NumberFormat (ES2020) internally.

Example

import { getNumberFormatter } from '@messageformat/number-skeleton'

let src = ':: currency/CAD unit-width-narrow'
let fmt = getNumberFormatter('en-CA', src, console.error)
fmt(42) // '$42.00'

src = '::percent scale/100'
fmt = getNumberFormatter('en', src, console.error)
fmt(0.3) // '30%'