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 = 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 = NumberFormat.percentPattern("ar");
  var eurosInUSFormat = NumberFormat.currency(locale: "en_US",
      symbol: "€");

There are several such constructors available, though some of them are limited. For example, at the moment, scientificPattern prints only as equivalent to "#E0" and does not take into account significant digits.

Constructors

NumberFormat([String? newPattern, String? locale])
Create a number format that prints using newPattern as it applies in locale.
factory
NumberFormat.compact({String? locale, bool explicitSign = false})
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, bool explicitSign = false})
A number format for "long" compact representations, e.g. "1.2 million" instead 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.
factory
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.
factory
NumberFormat.decimalPatternDigits({String? locale, int? decimalDigits})
Create a number format that prints as DECIMAL_PATTERN.
factory
NumberFormat.decimalPercentPattern({String? locale, int? decimalDigits})
Create a number format that prints as PERCENT_PATTERN.
factory
NumberFormat.percentPattern([String? locale])
Create a number format that prints as PERCENT_PATTERN.
factory
NumberFormat.scientificPattern([String? locale])
Create a number format that prints as SCIENTIFIC_PATTERN.
factory
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.
getter/setter pair
currencySymbol String
The symbol to be used when formatting this as currency.
final
decimalDigits int?
The number of decimal places to use when formatting.
final
hashCode int
The hash code for this object.
no setterinherited
locale String
Return the locale code in which we operate, e.g. 'en_US' or 'pt'.
no setter
localeZero int
The code point for the locale's zero digit.
final
maximumFractionDigits int
getter/setter pair
maximumIntegerDigits int
getter/setter pair
maximumSignificantDigits int?
getter/setter pair
minimumExponentDigits int
getter/setter pair
minimumFractionDigits int
getter/setter pair
minimumIntegerDigits int
getter/setter pair
minimumSignificantDigits int?
getter/setter pair
minimumSignificantDigitsStrict bool
Whether minimumSignificantDigits should cause trailing 0 in fraction part.
getter/setter pair
multiplier int
For percent and permille, what are we multiplying by in order to get the printed value, e.g. 100 for percent.
final
negativePrefix String
Variables to determine how number printing behaves.
final
negativeSuffix String
final
positivePrefix String
final
positiveSuffix String
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
significantDigits int?
How many significant digits should we print.
getter/setter pair
significantDigitsInUse bool
getter/setter pair
symbols → NumberSymbols
Return the symbols which are used in our locale. Cache them to avoid repeated lookup.
no setter

Methods

format(dynamic number) String
Format number according to our pattern and return the formatted string.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
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
A string representation of this object.
override
turnOffGrouping() → void
Explicitly turn off any grouping (e.g. by thousands) in this format.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

localeExists(String? 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