The
divide(long p, long q, RoundingMode mode) method of Guava's
LongMath Class accepts three parameters and calculates the result of dividing first parameter by second parameter, rounded according to the rounding mode specified by the third parameter.
Syntax:
public static long divide(long p,
long q,
RoundingMode mode)
Parameters: The method takes 3 parameters, where
p and
q are longs and
mode is a specified rounding mode.
Return Value: This method returns division of first parameter by second parameter, rounded according to the rounding mode specified by the third parameter.
Exceptions: The method throws
ArithmeticException if:
- q == 0, or
- mode == UNNECESSARY and p is not an integer multiple of q.
Enum RoundingMode
| Enum Constant |
Description |
| 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 illustrate the implementation of above method:
Example 1 :
Example 2 :