dojo/number

Summary

localized formatting and parsing routines for Number

See the dojo/number reference documentation for more information.

Properties

Methods

format(value,options)

Defined by dojo/number

Format a Number as a String, using locale-specific settings

Create a string from a Number using a known localized pattern. Formatting patterns appropriate to the locale are chosen from the Common Locale Data Repository as well as the appropriate symbols and delimiters. If value is Infinity, -Infinity, or is not a valid JavaScript number, return null.

Parameter Type Description
value Number

the number to be formatted

options Object
Optional

An object with the following properties:

  • pattern (String, optional):

    override formatting pattern with this string. Default value is based on locale. Overriding this property will defeat localization. Literal characters in patterns are not supported.

  • type (String, optional):

    choose a format type based on the locale from the following: decimal, scientific (not yet supported), percent, currency. decimal by default.

  • places (Number, optional):

    fixed number of decimal places to show. This overrides any information in the provided pattern.

  • round (Number, optional):

    5 rounds to nearest .5; 0 rounds to nearest whole (default). -1 means do not round.

  • locale (String, optional):

    override the locale used to determine formatting rules

  • fractional (Boolean, optional):

    If false, show no decimal places, overriding places and pattern settings.

Returns: null | undefined

parse(expression,options)

Defined by dojo/number

Convert a properly formatted string to a primitive Number, using locale-specific settings.

Create a Number from a string using a known localized pattern. Formatting patterns are chosen appropriate to the locale and follow the syntax described by unicode.org TR35 Note that literal characters in patterns are not supported.

Parameter Type Description
expression String

A string representation of a Number

options Object
Optional

An object with the following properties:

  • pattern (String, optional):

    override formatting pattern with this string. Default value is based on locale. Overriding this property will defeat localization. Literal characters in patterns are not supported.

  • type (String, optional):

    choose a format type based on the locale from the following: decimal, scientific (not yet supported), percent, currency. decimal by default.

  • locale (String, optional):

    override the locale used to determine formatting rules

  • strict (Boolean, optional):

    strict parsing, false by default. Strict parsing requires input as produced by the format() method. Non-strict is more permissive, e.g. flexible on white space, omitting thousands separators

  • fractional (Boolean|Array, optional):

    Whether to include the fractional portion, where the number of decimal places are implied by pattern or explicit 'places' parameter. The value [true,false] makes the fractional portion optional.

Returns: number

regexp(options)

Defined by dojo/number

Builds the regular needed to parse a number

Returns regular expression with positive and negative match, group and decimal separators

Parameter Type Description
options Object
Optional

An object with the following properties:

  • pattern (String, optional):

    override formatting pattern with this string. Default value is based on locale. Overriding this property will defeat localization.

  • type (String, optional):

    choose a format type based on the locale from the following: decimal, scientific (not yet supported), percent, currency. decimal by default.

  • locale (String, optional):

    override the locale used to determine formatting rules

  • strict (Boolean, optional):

    strict parsing, false by default. Strict parsing requires input as produced by the format() method. Non-strict is more permissive, e.g. flexible on white space, omitting thousands separators

  • places (Number|String, optional):

    number of decimal places to accept: Infinity, a positive number, or a range "n,m". Defined by pattern or Infinity if pattern not provided.

Returns: undefined

round(value,places,increment)

Defined by dojo/number

Rounds to the nearest value with the given number of decimal places, away from zero

Rounds to the nearest value with the given number of decimal places, away from zero if equal. Similar to Number.toFixed(), but compensates for browser quirks. Rounding can be done by fractional increments also, such as the nearest quarter. NOTE: Subject to floating point errors. See dojox/math/round for experimental workaround.

Parameter Type Description
value Number

The number to round

places Number
Optional

The number of decimal places where rounding takes place. Defaults to 0 for whole rounding. Must be non-negative.

increment Number
Optional

Rounds next place to nearest value of increment/10. 10 by default.

Returns: number

Examples

Example 1

>>> number.round(-0.5)
-1
>>> number.round(162.295, 2)
162.29  // note floating point error.  Should be 162.3
>>> number.round(10.71, 0, 2.5)
10.75

© 2005–2017 JS Foundation
Licensed under the AFL 2.1 and BSD 3-Clause licenses.
http://dojotoolkit.org/api/1.10/dojo/number.html