toNearestDecimal method

Rational toNearestDecimal(
  1. int decimalPlaces, {
  2. RoundingMode mode = RoundingMode.halfUp,
})

Rounds the rational number to the nearest decimal place

Returns a Rational value rounded to the specified number of decimal places. For formatted string output, see RationalFormattingExtension.toDecimalString.

Parameters:

  • decimalPlaces: The number of decimal places to round to
  • mode: The rounding mode to apply. Defaults to RoundingMode.halfUp

Returns: The rational number rounded to the nearest decimal place (as a Rational)

See also:

Implementation

Rational toNearestDecimal(int decimalPlaces, {RoundingMode mode = RoundingMode.halfUp}) {
  if (decimalPlaces < 0) {
    throw ArgumentError('The number of decimal places must be non-negative.');
  }
  final scale = Rational(BigInt.from(10).pow(decimalPlaces));
  return (this * scale).rounded(mode) / scale;
}