asFixed method

String asFixed(
  1. int precision, {
  2. bool remove_trailing_zeros = true,
})

Implementation

String asFixed(int precision, {bool remove_trailing_zeros = true}) {
  // precision is the number of decimal places after the decimal point to keep
  var offset = _decimal + precision - 1;
  var extra_zeroes = precision - (_digits.length - offset);

  if (extra_zeroes > 0) {
    _digits.addAll(
        Formatter.get_padding(extra_zeroes, '0').split('').map(int.parse));
  }

  _round(offset + 1, offset);

  var ret = _digits.sublist(0, _decimal).fold('', (i, e) => '${i}${e}');
  var trailing_digits = _digits.sublist(_decimal, _decimal + precision);
  if (remove_trailing_zeros) {
    trailing_digits = _remove_trailing_zeros(trailing_digits);
  }
  var trailing_zeroes = trailing_digits.fold('', (i, e) => '${i}${e}');
  if (trailing_zeroes.isEmpty) {
    return ret;
  }
  ret = '${ret}.${trailing_zeroes}';

  return ret;
}