Class

NumFmt

NumFmt(options)

Create a new number formatter instance. Locales differ in the way that digits in a formatted number are grouped, in the way the decimal character is represented, etc. Use this formatter to get it right for any locale.

This formatter can format plain numbers, currency amounts, and percentage amounts.

As with all formatters, the recommended practice is to create one formatter and use it multiple times to format various numbers.

The options can contain any of the following properties:

  • locale - use the conventions of the specified locale when figuring out how to format a number.
  • type - the type of this formatter. Valid values are "number", "currency", or "percentage". If this property is not specified, the default is "number".
  • currency - the ISO 4217 3-letter currency code to use when the formatter type is "currency". This property is required for currency formatting. If the type property is "currency" and the currency property is not specified, the constructor will throw a an exception.
  • maxFractionDigits - the maximum number of digits that should appear in the formatted output after the decimal. A value of -1 means unlimited, and 0 means only print the integral part of the number.
  • minFractionDigits - the minimum number of fractional digits that should appear in the formatted output. If the number does not have enough fractional digits to reach this minimum, the number will be zero-padded at the end to get to the limit. If the type of the formatter is "currency" and this property is not specified, then the minimum fraction digits is set to the normal number of digits used with that currency, which is almost always 0, 2, or 3 digits.
  • significantDigits - specify that max number of significant digits in the formatted output. This applies before and after the decimal point. The amount is rounded according to the rounding mode specified, or the rounding mode as given in the locale information. If the significant digits and the max or min fraction digits are both specified, this formatter will attempt to honour them both by choosing the one that is smaller if there is a conflict. For example, if the max fraction digits is 6 and the significant digits is 5 and the number to be formatted has a long fraction, it will only format 5 digits. The default is "unlimited digits", which means to format as many digits as the javascript engine can represent internally (usually around 13-15 or so on a 64-bit machine).
  • useNative - the flag used to determaine whether to use the native script settings for formatting the numbers .
  • roundingMode - When the maxFractionDigits or maxIntegerDigits is specified, this property governs how the least significant digits are rounded to conform to that maximum. The value of this property is a string with one of the following values:
    • up - round away from zero
    • down - round towards zero. This has the effect of truncating the number
    • ceiling - round towards positive infinity
    • floor - round towards negative infinity
    • halfup - round towards nearest neighbour. If equidistant, round up.
    • halfdown - round towards nearest neighbour. If equidistant, round down.
    • halfeven - round towards nearest neighbour. If equidistant, round towards the even neighbour
    • halfodd - round towards nearest neighbour. If equidistant, round towards the odd neighbour
    When the type of the formatter is "currency" and the roundingMode property is not set, then the standard legal rounding rules for the locale are followed. If the type is "number" or "percentage" and the roundingMode property is not set, then the default mode is "halfdown"..
  • style - When the type of this formatter is "currency", the currency amount can be formatted in the following styles: "common" and "iso". The common style is the one commonly used in every day writing where the currency unit is represented using a symbol. eg. "$57.35" for fifty-seven dollars and thirty five cents. The iso style is the international style where the currency unit is represented using the ISO 4217 code. eg. "USD 57.35" for the same amount. The default is "common" style if the style is not specified.

    When the type of this formatter is "number", the style can be one of the following:
    • standard - format a fully specified floating point number properly for the locale
    • scientific - use scientific notation for all numbers. That is, 1 integral digit, followed by a number of fractional digits, followed by an "e" which denotes exponentiation, followed digits which give the power of 10 in the exponent.
    • native - format a floating point number using the native digits and formatting symbols for the script of the locale.
    • nogrouping - format a floating point number without grouping digits for the integral portion of the number
    Note that if you specify a maximum number of integral digits, the formatter with a standard style will give you standard formatting for smaller numbers and scientific notation for larger numbers. The default is standard style if this is not specified.
  • onLoad - a callback function to call when the format data is fully loaded. When the onLoad option is given, this class will attempt to load any missing locale data using the ilib loader callback. When the constructor is done (even if the data is already preassembled), the onLoad function is called with the current instance as a parameter, so this callback can be used with preassembled or dynamic loading or a mix of the two.
  • sync - tell whether to load any missing locale data synchronously or asynchronously. If this option is given as "false", then the "onLoad" callback must be given, as the instance returned from this constructor will not be usable for a while.
  • loadParams - an object containing parameters to pass to the loader callback function when locale data is missing. The parameters are not interpretted or modified in any way. They are simply passed along. The object may contain any property/value pairs as long as the calling code is in agreement with the loader callback function as to what those parameters mean.

Constructor

# new NumFmt(options)

Parameters:
Name Type Description
options Object.<string, *>

A set of options that govern how the formatter will behave

View Source NumFmt.js, line 151

Methods

# constrain(num) → {number}

Apply the constraints used in the current formatter to the given number. This will will apply the maxFractionDigits, significantDigits, and rounding mode constraints and return the result. The result is further manipulated in the format method to produce the final formatted number string. This method is intended for use by code that needs to use the same number that this formatter instance uses for formatting before that number is turned into a formatted string.

Parameters:
Name Type Description
num number

the number to constrain

View Source NumFmt.js, line 402

the number with the constraints applied to it

number

# format(num) → {string}

Format a number according to the settings of this number formatter instance.

Parameters:
Name Type Description
num number | string | INumber | Number

a floating point number to format

View Source NumFmt.js, line 549

a string containing the formatted number

string

# getCurrency() → {string}

Returns the ISO 4217 code for the currency that this formatter formats. IF the typeof this formatter is not "currency", then this method will return undefined.

View Source NumFmt.js, line 651

the ISO 4217 code for the currency that this formatter formats, or undefined if this not a currency formatter

string

# getLocale() → {Locale}

Return the locale for this formatter instance.

View Source NumFmt.js, line 593

the locale instance for this formatter

Locale

# getMaxFractionDigits() → {number}

Returns the maximum fraction digits set up in the constructor.

View Source NumFmt.js, line 616

the maximum number of fractional digits this formatter will format, or -1 for no maximum

number

# getMinFractionDigits() → {number}

Returns the minimum fraction digits set up in the constructor. If the formatter has the type "currency", then the minimum fraction digits is the amount of digits that is standard for the currency in question unless overridden in the options to the constructor.

View Source NumFmt.js, line 629

the minimum number of fractional digits this formatter will format, or -1 for no minimum

number

# getRoundingMode() → {string}

Returns the rounding mode set up in the constructor. The rounding mode controls how numbers are rounded when the integral or fraction digits of a number are limited.

View Source NumFmt.js, line 662

the name of the rounding mode used in this formatter

string

# getSignificantDigits() → {number}

Returns the significant digits set up in the constructor.

View Source NumFmt.js, line 639

the number of significant digits this formatter will format, or -1 for no minimum

number

# getStyle() → {string}

If this formatter is a currency formatter, then the style determines how the currency is denoted in the formatted output. This method returns the style that this formatter will produce. (See the constructor comment for more about the styles.)

View Source NumFmt.js, line 674

the name of the style this formatter will use to format currency amounts, or "undefined" if this formatter is not a currency formatter

string

# getType() → {string}

Return the type of formatter. Valid values are "number", "currency", and "percentage".

View Source NumFmt.js, line 585

the type of formatter

string

# getUseNative() → {boolean}

Return true if this formatter uses native digits to format the number. If the useNative option is given to the constructor, then this flag will be honoured. If the useNative option is not given to the constructor, this this formatter will use native digits if the locale typically uses native digits.

View Source NumFmt.js, line 340

true if this formatter will format with native digits, false otherwise

boolean

# isGroupingUsed() → {boolean}

Returns true if this formatter groups together digits in the integral portion of a number, based on the options set up in the constructor. In most western European cultures, this means separating every 3 digits of the integral portion of a number with a particular character.

View Source NumFmt.js, line 606

true if this formatter groups digits in the integral portion of the number

boolean

# static getAvailableLocales() → {Array.<Locale>|undefined}

Return an array of available locales that this formatter can format

View Source NumFmt.js, line 320

an array of available locales

Array.<Locale> | undefined