roundedDiv method

BigInt roundedDiv(
  1. BigInt denominator,
  2. RoundingMode mode
)

Divides this by denominator and rounds the result using the specified mode

The rounding behavior varies based on the mode:

  • floor: Rounds down to the next lower integer
  • ceil: Rounds up to the next higher integer
  • truncate: Rounds towards zero (removes the fractional part)
  • up: Rounds away from zero, increasing the magnitude of the quotient
  • halfUp: Rounds to the nearest integer, rounding up on ties
  • halfDown: Rounds to the nearest integer, rounding down on ties
  • halfEven: Rounds to the nearest integer; if exactly halfway, rounds to the nearest even integer

Example:

print(BigInt.from(11).roundedDiv(BigInt.from(3), RoundingMode.floor));     // 11 / 3  --> 3
print(BigInt.from(10).roundedDiv(BigInt.from(3), RoundingMode.ceil));      // 10 / 3  --> 4
print(BigInt.from(11).roundedDiv(BigInt.from(3), RoundingMode.truncate));  // 11 / 3  --> 3
print(BigInt.from(-10).roundedDiv(BigInt.from(3), RoundingMode.truncate)); // -11 / 3 --> -3
print(BigInt.from(10).roundedDiv(BigInt.from(3), RoundingMode.up));        // 10 / 3  --> 4
print(BigInt.from(-10).roundedDiv(BigInt.from(2), RoundingMode.up));       // -10 / 3 --> -4
print(BigInt.from(7).roundedDiv(BigInt.from(2), RoundingMode.halfUp));     // 7 / 2   --> 4
print(BigInt.from(7).roundedDiv(BigInt.from(2), RoundingMode.halfDown));   // 7 / 2   --> 3
print(BigInt.from(7).roundedDiv(BigInt.from(2), RoundingMode.halfEven));   // 7 / 2   --> 4
print(BigInt.from(5).roundedDiv(BigInt.from(2), RoundingMode.halfEven));   // 5 / 2   --> 2
print(BigInt.from(6).roundedDiv(BigInt.from(2), RoundingMode.halfEven));   // 6 / 2   --> 3
  • denominator: The divisor
  • mode: The rounding mode to apply
  • Returns: The rounded quotient as a BigInt

Implementation

BigInt roundedDiv(BigInt denominator, RoundingMode mode) {
  final Rational value = Rational(this, denominator);
  return value.rounded(mode).toBigInt();
}