Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
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 :
// Java code to show implementation of // divide(long p, long q, RoundingMode mode) // method of Guava's LongMath class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { // Driver code public static void main(String args[]) { long dividend1 = 55; long divisor1 = 10; // Using divide(long p, long q, // RoundingMode mode) // method of Guava's LongMath class long quotient1 = LongMath.divide(dividend1, divisor1, RoundingMode.HALF_DOWN); System.out.println(quotient1); long dividend2 = 55; long divisor2 = 10; // Using divide(long p, long q, // RoundingMode mode) // method of Guava's LongMath class long quotient2 = LongMath.divide(dividend2, divisor2, RoundingMode.CEILING); System.out.println(quotient2); } } |
5 6
Example 2 :
// Java code to show implementation of // divide(long p, long q, RoundingMode mode) // method of Guava's LongMath class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { static long findDiv(long dividend, long divisor, RoundingMode mode) { try { // Using divide(long p, long q, // RoundingMode mode) // method of Guava's LongMath class long quotient = LongMath.divide(dividend, divisor, RoundingMode.HALF_DOWN); // Return the answer return quotient; } catch (Exception e) { System.out.println(e); return -1; } } // Driver code public static void main(String args[]) { long dividend = 32; long divisor = 0; try { // Function calling findDiv(dividend, divisor, RoundingMode.HALF_EVEN); } catch (Exception e) { System.out.println(e); } } } |
java.lang.ArithmeticException: / by zero
Recommended Posts:
- Java Guava | LongMath log10(long x, RoundingMode mode) method with Examples
- Java Guava | log2(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | IntMath.divide(int, int, RoundingMode) method with Examples
- Java Guava | IntMath log10(int x, RoundingMode mode) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
- Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
- Java Guava | Longs.indexOf(long[] array, long target) method with Examples
- Java Guava | LongMath.checkedPow(long b, int k) method with Examples
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
- Java Guava | mod(long x, long m) of LongMath Class with Examples
- Java Guava | log2(int x, RoundingMode mode) method of IntMath
- Java Guava | sqrt(int x, RoundingMode mode) method of IntMath Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



