formatCurrency method

String formatCurrency({
  1. String symbol = '\$',
  2. int decimals = 2,
})

Formats the number as currency.

Example:

1234.formatCurrency(); // '\$1,234.00'
1234.formatCurrency(symbol: '€'); // '€1,234.00'
1234.formatCurrency(decimals: 0); // '\$1,234'

Implementation

String formatCurrency({String symbol = '\$', int decimals = 2}) {
  assert(decimals >= 0, 'decimals must be non-negative');
  // Integers have no fractional part, so the decimals are always zeros.
  final String fraction = decimals > 0 ? '.${'0' * decimals}' : '';
  return '$symbol${formatWithSeparator()}$fraction';
}