graphGrouped function

String graphGrouped(
  1. String number
)

A number as written, with a comma between its thousands: 1234.5 as 1,234.5.

Implementation

String graphGrouped(String number) {
  final negative = number.startsWith('-');
  final digits = negative ? number.substring(1) : number;
  final dot = digits.indexOf('.');
  final whole = dot < 0 ? digits : digits.substring(0, dot);
  final out = StringBuffer();
  for (var i = 0; i < whole.length; i++) {
    if (i > 0 && (whole.length - i) % 3 == 0) out.write(',');
    out.write(whole[i]);
  }
  return '${negative ? '-' : ''}$out${dot < 0 ? '' : digits.substring(dot)}';
}