getCurrencyNumberFormat function

NumberFormat getCurrencyNumberFormat({
  1. num? value,
  2. String? locale = 'en_US',
  3. String symbol = 'USD',
  4. int? minimumFractionDigits,
  5. int? maximumFractionDigits,
})

Returns a NumberFormat for currency that uses the locale, symbol, and specific fraction digits defined by the user.

  • value: The number to format, used to determine default fraction digits if they are not specified.
  • locale: The locale to use for formatting. Defaults to 'en_US'.
  • symbol: The currency symbol to use in formatting. Defaults to 'USD', and is converted to uppercase.
  • minimumFractionDigits: The minimum number of digits after the decimal to display. If not specified, a default is calculated based on value.
  • maximumFractionDigits: The maximum number of digits after the decimal to display. If not specified, a default is calculated based on value.

Implementation

NumberFormat getCurrencyNumberFormat({
  num? value,
  String? locale = 'en_US',
  String symbol = 'USD',
  int? minimumFractionDigits,
  int? maximumFractionDigits,
}) {
  final (minFractionDigits, maxFractionDigits) = getDefaultFractionDigits(
    value ?? 0,
    minimumFractionDigits,
    maximumFractionDigits,
  );

  symbol = symbol.toUpperCase();

  final formatter = NumberFormat.simpleCurrency(locale: locale, name: symbol)
    ..maximumFractionDigits = maxFractionDigits
    ..minimumFractionDigits = minFractionDigits;

  return formatter;
}