NumberFormat class

Provides the ability to format a number in a locale-specific way. The format is specified as a pattern using a subset of the ICU formatting patterns.

  • 0 A single digit
  • # A single digit, omitted if the value is zero
  • . Decimal separator
  • - Minus sign
  • , Grouping separator
  • E Separates mantissa and expontent
  • + - Before an exponent, to say it should be prefixed with a plus sign.
  • % - In prefix or suffix, multiply by 100 and show as percentage
  • ‰ (\u2030) In prefix or suffix, multiply by 1000 and show as per mille
  • ¤ (\u00A4) Currency sign, replaced by currency name
  • ' Used to quote special characters
  • ; Used to separate the positive and negative patterns (if both present)

For example, var f = new NumberFormat("###.0#", "en_US"); print(f.format(12.345)); ==> 12.34 If the locale is not specified, it will default to the current locale. If the format is not specified it will print in a basic format with at least one integer digit and three fraction digits.

There are also standard patterns available via the special constructors. e.g. var percent = new NumberFormat.percentFormat("ar"); var eurosInUSFormat = new NumberFormat.currency(locale: "en_US", symbol: "€"); There are four such constructors: decimalFormat, percentFormat, scientificFormat and currencyFormat. However, at the moment, scientificFormat prints only as equivalent to "#E0" and does not take into account significant digits. The currencyFormat will default to the three-letter name of the currency if no explicit name/symbol is provided.


NumberFormat([String newPattern, String locale])
Create a number format that prints using newPattern as it applies in locale.
NumberFormat.compact({String locale})
A number format for compact representations, e.g. "1.2M" instead of "1,200,000".
NumberFormat.compactCurrency({String locale, String name, String symbol, int decimalDigits})
A number format for compact currency representations, e.g. "$1.2M" instead of "$1,200,000".
NumberFormat.compactLong({String locale})
A number format for "long" compact representations, e.g. "1.2 million" instead of of "1,200,000".
NumberFormat.compactSimpleCurrency({String locale, String name, int decimalDigits})
A number format for compact currency representations, e.g. "$1.2M" instead of "$1,200,000", and which will automatically determine a currency symbol based on the currency name or the locale. See NumberFormat.simpleCurrency.
NumberFormat.currency({String locale, String name, String symbol, int decimalDigits, String customPattern})
Create a NumberFormat that formats using the locale's CURRENCY_PATTERN. [...]
NumberFormat.currencyPattern([String locale, String currencyNameOrSymbol])
Create a number format that prints as CURRENCY_PATTERN. (Deprecated: prefer NumberFormat.currency) [...]
NumberFormat.decimalPattern([String locale])
Create a number format that prints as DECIMAL_PATTERN.
NumberFormat.percentPattern([String locale])
Create a number format that prints as PERCENT_PATTERN.
NumberFormat.scientificPattern([String locale])
Create a number format that prints as SCIENTIFIC_PATTERN.
NumberFormat.simpleCurrency({String locale, String name, int decimalDigits})
Creates a NumberFormat for currencies, using the simple symbol for the currency if one is available (e.g. $, €), so it should only be used if the short currency symbol will be unambiguous. [...]


currencyName String
The name of the currency to print, in ISO 4217 form.
read / write
currencySymbol String
The symbol to be used when formatting this as currency. [...]
decimalDigits int
The number of decimal places to use when formatting. [...]
hashCode int
The hash code for this object. [...]
read-only, inherited
locale String
Return the locale code in which we operate, e.g. 'en_US' or 'pt'.
maximumFractionDigits int
read / write
maximumIntegerDigits int
read / write
minimumExponentDigits int
read / write
minimumFractionDigits int
read / write
minimumIntegerDigits int
read / write
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
significantDigits int
How many significant digits should we print. [...]
read / write
significantDigitsInUse bool
read / write
symbols NumberSymbols
Return the symbols which are used in our locale. Cache them to avoid repeated lookup.


format(dynamic number) String
Format number according to our pattern and return the formatted string.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
parse(String text) num
Parse the number represented by the string. If it's not parseable, throws a FormatException.
simpleCurrencySymbol(String currencyCode) String
Returns the simple currency symbol for given currency code, or currencyCode if no simple symbol is listed. [...]
toString() String
Returns a string representation of this object.
turnOffGrouping() → void
Explicitly turn off any grouping (e.g. by thousands) in this format. [...]


operator ==(Object other) bool
The equality operator. [...]

Static Methods

localeExists(dynamic localeName) bool
Return true if the locale exists, or if it is null. The null case is interpreted to mean that we use the default locale.
numberOfIntegerDigits(dynamic number) int