formatWithCommasDouble static method

String formatWithCommasDouble(
  1. double number, {
  2. int decimalPlaces = 2,
})

Format number with commas (double)

number - The number to format decimalPlaces - Number of decimal places (default: 2) Returns formatted number string

Implementation

static String formatWithCommasDouble(double number, {int decimalPlaces = 2}) {
  final formatter = RegExp(r'(\d)(?=(\d{3})+(?!\d))');
  final formatted = number.toStringAsFixed(decimalPlaces);
  final parts = formatted.split('.');
  final integerPart = parts[0].replaceAllMapped(formatter, (match) => '${match.group(1)},');
  return decimalPlaces > 0 ? '$integerPart.${parts[1]}' : integerPart;
}