toStringAsPrecisionNoRound method
String
toStringAsPrecisionNoRound(
- int precision
)
Implementation
String toStringAsPrecisionNoRound(int precision) {
assert(precision > 0);
if (this == Decimal.zero) {
return <String>[
'0',
if (precision > 1) '.',
for (var i = 1; i < precision; i++) '0',
].join();
}
final limit = Decimal.ten.pow(precision).toDecimal();
var shift = Decimal.one;
final absValue = abs();
var pad = 0;
while (absValue * shift < limit) {
pad++;
shift *= Decimal.ten;
}
while (absValue * shift >= limit) {
pad--;
shift = (shift / Decimal.ten).toDecimal();
}
final value = ((this * shift).floor() / shift).toDecimal();
return pad <= 0 ? value.toString() : value.toStringAsFixed(pad);
}