The method divide(BigInteger p, BigInteger q, RoundingMode mode) of Guava’s BigIntegerMath class returns the result of dividing p by q, rounding using the specified RoundingMode.
public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode)
Parameters: This method takes the following parameters:
- p: The BigInteger dividend
- q: The BigInteger divisor
- mode: The rounding mode for calculating the division of p and q.
Return Value: This method returns the result of dividing p by q, rounding using the specified RoundingMode.
Exceptions: The method throws ArithmeticException if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b.
|CEILING||Rounding mode to round towards positive infinity.|
|DOWN||Rounding mode to round towards zero.|
|FLOOR||Rounding mode to round towards negative infinity.|
|HALF_DOWN||Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.|
|HALF_EVEN||Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.|
|HALF_UP||Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.|
|UNNECESSARY||Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.|
|UP||Rounding mode to round away from zero.|
Below examples illustrates the BigIntegerMath.divide() method:
55 divided by 10 with HALF_DOWN rounding mode: 5 55 divided by 10 with CEILING rounding mode: 6
Exception: java.lang.ArithmeticException: / by zero
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.