toDecimal method
Returns a Decimal corresponding to this.
Some rational like 1/3 can not be converted to decimal because they need
an infinite number of digits. For those cases (where hasFinitePrecision
is false) a scaleOnInfinitePrecision and a toBigInt can be provided
to convert this to a Decimal representation. By default toBigInt
use Rational.truncate to limit the number of digit.
Note that the returned decimal will not be exactly equal to this.
Implementation
Decimal toDecimal({
int? scaleOnInfinitePrecision,
BigInt Function(Rational)? toBigInt,
}) {
if (hasFinitePrecision) {
var scale = _scale;
return Decimal._((this * Rational(_i10.pow(scale))).toBigInt(), scale);
}
if (scaleOnInfinitePrecision == null) {
throw AssertionError(
'scaleOnInfinitePrecision is required for rationale without finite precision');
}
final scaleFactor = _r10.pow(scaleOnInfinitePrecision);
toBigInt ??= (value) => value.truncate();
return Decimal._(toBigInt(this * scaleFactor), scaleOnInfinitePrecision);
}