toDecimal method

Decimal toDecimal({
  1. int? scaleOnInfinitePrecision,
  2. BigInt toBigInt(
    1. Rational
    )?,
})

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 (scaleOnInfinitePrecision == null || hasFinitePrecision) {
    return Decimal._(this);
  }
  final scaleFactor = _r10.pow(scaleOnInfinitePrecision);
  toBigInt ??= (value) => value.truncate();
  return Decimal._(toBigInt(this * scaleFactor).toRational() / scaleFactor);
}