toCommaString method

String toCommaString({
  1. String comma = ',',
})

takes a numeric string and inserts commas

Implementation

String toCommaString({String comma = ','}) {
  final List strs = replaceAll(',', '').split('.');
  final String str = strs[0];
  int i = 0;
  String ret = '';
  for (final String c in str.characters.reversed) {
    if (i == 3) {
      ret = '$c$comma$ret';
      i = 1;
    } else {
      ret = '$c$ret';
      i += 1;
    }
  }
  if (contains('.')) {
    return '$ret.${strs.last}';
  } else {
    return ret;
  }
}