toCurrency method

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

Formats this integer as currency.

Example:

int price = 1234;
String formatted = price.toCurrency(); // '$1,234.00'

Implementation

String toCurrency({String symbol = '\$', int decimals = 2}) {
  return '$symbol${(this / 1).toStringAsFixed(decimals).replaceAllMapped(
        RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'),
        (Match m) => '${m[1]},',
      )}';
}