toCurrencyString method

String toCurrencyString([
  1. String delimiter = ',',
  2. int precision = 2
])

Convert to currency string with specified delimiter and precision. If delimiter is not specified, the default is ','. If precision is not specified, the default is 2.

Implementation

String toCurrencyString([String delimiter = ',', int precision = 2]) {
  var result1 = toPrecision(precision);
  var parts = result1.split('.');
  var integer = parts[0];
  var decimal = parts[1];
  var result = '';
  var count = 0;
  for (var i = integer.length - 1; i >= 0; i--) {
    result = integer[i] + result;
    count++;
    if (count == 3 && i != 0) {
      result = delimiter + result;
      count = 0;
    }
  }
  return '$result.$decimal';
}