asFixed method
String
asFixed(
- int precision, {
- bool removeTrailingZeros = true,
})
Implementation
String asFixed(int precision, {bool removeTrailingZeros = true}) {
// precision is the number of decimal places after the decimal point to keep
int offset = _decimal + precision - 1;
int extraZeroes = precision - (_digits.length - offset);
if (extraZeroes > 0) {
_digits.addAll(
Formatter.getPadding(extraZeroes, '0').split('').map(int.parse));
}
_round(offset + 1, offset);
String ret = _digits.sublist(0, _decimal).fold('', (i, e) => "$i$e");
List<int> trailingDigits = _digits.sublist(_decimal, _decimal + precision);
if (removeTrailingZeros) {
trailingDigits = _removeTrailingZeros(trailingDigits);
}
var trailingZeroes = trailingDigits.fold('', (i, e) => "$i$e");
if (trailingZeroes.length == 0) {
return ret;
}
ret = "$ret.$trailingZeroes";
return ret;
}