NumberFormat.currency constructor

NumberFormat.currency({
  1. String? locale,
  2. String? name,
  3. String? symbol,
  4. int? decimalDigits,
  5. String? customPattern,
})

Create a NumberFormat that formats using the locale's CURRENCY_PATTERN.

If locale is not specified, it will use the current default locale.

If name is specified, the currency with that ISO 4217 name will be used. Otherwise we will use the default currency name for the current locale. If no symbol is specified, we will use the currency name in the formatted result. e.g. var f = NumberFormat.currency(locale: 'en_US', name: 'EUR') will format currency like "EUR1.23". If we did not specify the name, it would format like "USD1.23".

If symbol is used, then that symbol will be used in formatting instead of the name. e.g. var eurosInCurrentLocale = NumberFormat.currency(symbol: "€"); will format like "€1.23". Otherwise it will use the currency name. If this is not explicitly specified in the constructor, then for currencies we use the default value for the currency if the name is given, otherwise we use the value from the pattern for the locale.

If decimalDigits is specified, numbers will format with that many digits after the decimal place. If it's not, they will use the default for the currency in name, and the default currency for locale if the currency name is not specified. e.g. NumberFormat.currency(name: 'USD', decimalDigits: 7) will format with 7 decimal digits, because that's what we asked for. But NumberFormat.currency(locale: 'en_US', name: 'JPY') will format with zero, because that's the default for JPY, and the currency's default takes priority over the locale's default. NumberFormat.currency(locale: 'en_US') will format with two, which is the default for that locale.

The customPattern parameter can be used to specify a particular format. This is useful if you have your own locale data which includes unsupported formats (e.g. accounting format for currencies.)

Implementation

// TODO(alanknight): Should we allow decimalDigits on other numbers.
factory NumberFormat.currency(
        {String? locale,
        String? name,
        String? symbol,
        int? decimalDigits,
        String? customPattern}) =>
    NumberFormat._forPattern(
        locale, (x) => customPattern ?? x.CURRENCY_PATTERN,
        name: name,
        currencySymbol: symbol,
        decimalDigits: decimalDigits,
        isForCurrency: true);