formatCurrency static method

String formatCurrency(
  1. double value
)

Implementation

static String formatCurrency(double value) {
  String raw = value.toStringAsFixed(2);
  String symbol = 'R\$ ';
  List<String> parts = raw.split('.');

  String decimalPart = parts.last;
  String integerPart = parts.first;

  final buffer = StringBuffer()
    ..write(symbol)
    ..write(integerPart.replaceAllMapped(RegExp(r'(\d)(?=(\d{3})+(?!\d))'),
        (Match match) => '${match.group(0)},'))
    ..write(',')
    ..write(decimalPart);

  return buffer.toString();
}