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.

Constructors

NumberFormat([String newPattern, String locale ])
Create a number format that prints using newPattern as it applies in locale.
factory
NumberFormat.compact({String locale })
A number format for compact representations, e.g. "1.2M" instead of "1,200,000".
factory
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".
factory
NumberFormat.compactLong({String locale })
A number format for "long" compact representations, e.g. "1.2 million" instead of of "1,200,000".
factory
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.
factory
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) [...]
factory
NumberFormat.decimalPattern([String locale ])
Create a number format that prints as DECIMAL_PATTERN.
NumberFormat.decimalPercentPattern({String locale, int decimalDigits })
Create a number format that prints as PERCENT_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. [...]
factory

Properties

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. [...]
read-only
decimalDigits → int
The number of decimal places to use when formatting. [...]
read-only
locale → String
Return the locale code in which we operate, e.g. 'en_US' or 'pt'.
read-only
maximumFractionDigits ↔ int
read / write
maximumIntegerDigits ↔ int
read / write
minimumExponentDigits ↔ int
read / write
minimumFractionDigits ↔ int
read / write
minimumIntegerDigits ↔ int
read / write
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.
read-only
hashCode → int
The hash code for this object.
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

format(dynamic number) → String
Format number according to our pattern and return the formatted string.
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. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator.
inherited

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