Skip to main content
Version: 26.2.0

NumberFormatter

The NumberFormatter class provides comprehensive number formatting capabilities with support for decimal, currency, and percentage styles. It handles thousands separators, decimal places, units, negative signs, and multi-locale support.

Constructor

new NumberFormatter(style, options)

Parameters

PropertyTypeRequiredDescription
style'decimal' | 'currency' | 'percent'trueThe formatting style to use
optionsObjectfalseStyle-specific formatting options

Formatting Styles

Decimal Style

Format numbers with customizable decimal places, thousands separators, and units.

const formatter = new NumberFormatter('decimal', {
  unit: 'auto',              // Display unit (none, auto, thousands, millions, billions, trillions)
  negativeSign: 'prefixDash', // How to display negative numbers
  thousandSeparator: 'comma', // Thousands separator (none, comma, period, space)
  thousandsGrouping: 3,       // Grouping size for thousands
  decimalSeparator: 'period', // Decimal separator (comma, period)
  decimalDigits: 2            // Number of decimal places
});

Decimal Options

OptionTypeDefaultDescription
unit'none' | 'auto' | 'thousands' | 'millions' | 'billions' | 'trillions''auto'Unit to apply to the number
negativeSign'prefixDash' | 'suffixDash' | 'parentheses''prefixDash'How to display negative numbers
thousandSeparator'none' | 'comma' | 'period' | 'space''comma'Character for thousands separator
thousandsGroupingnumber3Number of digits in each group
decimalSeparator'comma' | 'period''period'Character for decimal separator
decimalDigitsnumber2Number of decimal places to display

Currency Style

Format numbers as currency with support for different currency codes and formatting options.

const formatter = new NumberFormatter('currency', {
  currency: 'USD',            // ISO 4217 currency code
  unit: 'auto',               // Display unit
  negativeSign: 'prefixDash', // How to display negative numbers
  thousandSeparator: 'comma', // Thousands separator
  thousandsGrouping: 3,       // Grouping size
  decimalSeparator: 'period', // Decimal separator
  decimalDigits: 2            // Number of decimal places
});

Currency Options

OptionTypeDefaultDescription
currencystringBrowser defaultISO 4217 currency code (e.g., 'USD', 'EUR', 'GBP', 'INR')
unit'none' | 'auto' | 'thousands' | 'millions' | 'billions' | 'trillions''auto'Unit to apply to the number
negativeSign'prefixDash' | 'suffixDash' | 'parentheses''prefixDash'How to display negative numbers
thousandSeparator'none' | 'comma' | 'period' | 'space''comma'Character for thousands separator
thousandsGroupingnumber3Number of digits in each group
decimalSeparator'comma' | 'period''period'Character for decimal separator
decimalDigitsnumber2Number of decimal places to display

Percent Style

Format numbers as percentages (multiplies value by 100 and adds % symbol).

const formatter = new NumberFormatter('percent', {
  negativeSign: 'prefixDash',  // How to display negative numbers
  thousandSeparator: 'comma',  // Thousands separator
  thousandsGrouping: 3,        // Grouping size
  decimalSeparator: 'period',  // Decimal separator
  decimalDigits: 2             // Number of decimal places
});

Percent Options

OptionTypeDefaultDescription
negativeSign'prefixDash' | 'suffixDash' | 'parentheses''prefixDash'How to display negative numbers
thousandSeparator'none' | 'comma' | 'period' | 'space''comma'Character for thousands separator
thousandsGroupingnumber3Number of digits in each group
decimalSeparator'comma' | 'period''period'Character for decimal separator
decimalDigitsnumber2Number of decimal places to display

Methods

format()

Formats a single numeric value and returns the formatted string.

format(value: MuzeDatum): string

Parameters:

PropertyTypeRequiredDescription
valueMuzeDatumtrueThe numeric value to format

Returns: String - The formatted number

formatterFunc()

Returns a formatter function for use with Muze visualizations.

formatterFunc(): (dataInfo: MuzeType) => string

Returns: Function - A formatter function that can be passed to Muze configuration