Class NumberFormatter

A locale-specific number parser.

Remarks

This parser supports basic language parsing abilities, but can still parse unexpected results given the right input. For example:

NumberFormatter.forLcale("de").parse("1.23"); // returns 123

That example produces 123 instead of the (perhaps?) expected 1.23 because . is a thousands delimiter character in German and the parser simply removes that from the input, resulting in the string "123" that is then parsed into the number result 123.

Adapted from Mike Bostock's lovely code (thanks, Mike!).

Hierarchy

  • NumberFormatter

Constructors

Properties

#decimal: RegExp
#format: NumberFormat
#group: RegExp
#index: ((d) => string)

Type declaration

    • (d): string
    • Parameters

      • d: string

      Returns string

#locale: string
#numeral: RegExp

Accessors

Methods

  • Format a number into a string.

    This will return "NaN" if n is NaN or an empty string if n is undefined or null. Otherwise, n will be formatted with format if provided, falling back to a format with DEFAULT_FORMAT_OPTIONS.

    Parameters

    • n: number

      the number to format

    • Optional format: NumberFormat

      the format to use, or else a default format will be used

    Returns string

    the formatted number

  • Normalize a locale-specific number string.

    Parameters

    • s: string

      the number string to parse in this instance's locale

    Returns undefined | string

    the number string normalized into a JavaScript number string, or undefined if the normalized value is empty

  • Parse a locale-specific number string.

    Parameters

    • s: string

      the number string to parse in this instance's locale

    Returns number

    the parsed number, or undefined if s is undefined

  • Get a parser for a given locale.

    This method will instantiate and cache parsers, returning cached instances if already avaialble.

    Parameters

    • locale: string

      the locale of the parser to get

    Returns NumberFormatter

    the parser

Generated using TypeDoc