formatCurrency method

String formatCurrency(
  1. double amount,
  2. String currencyCode, {
  3. int decimalPlaces = 2,
})

Format currency amount with specified currency code Example: formatCurrency(1234.56, 'USD') returns "$1,234.56"

Implementation

String formatCurrency(
  double amount,
  String currencyCode, {
  int decimalPlaces = 2,
}) {
  final symbol = _getCurrencySymbol(currencyCode);
  final formattedNumber =
      amount.toStringAsFixed(decimalPlaces).replaceAllMapped(
            RegExp(r'(\d)(?=(\d{3})+(?!\d))'),
            (Match m) => '${m[1]},',
          );

  return '$symbol$formattedNumber';
}