Java Guava | IntMath.divide(int, int, RoundingMode) method with Examples Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report The divide(int p, int q, RoundingMode mode) method of Guava's IntMath 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 int divide(int p, int q, RoundingMode mode) Parameters: The method takes 3 parameters, where p and q are ints 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 // Java code to show implementation of // divide(int p, int q, RoundingMode mode) // method of Guava's IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { // Driver code public static void main(String args[]) { int dividend1 = 55; int divisor1 = 10; // Using divide(int p, int q, // RoundingMode mode) // method of Guava's IntMath class int quotient1 = IntMath.divide(dividend1, divisor1, RoundingMode.HALF_DOWN); System.out.println(quotient1); int dividend2 = 55; int divisor2 = 10; // Using divide(int p, int q, // RoundingMode mode) // method of Guava's IntMath class int quotient2 = IntMath.divide(dividend2, divisor2, RoundingMode.CEILING); System.out.println(quotient2); } } Output: 5 6 Example 2 : Java // Java code to show implementation of // divide(int p, int q, RoundingMode mode) // method of Guava's IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { static int findDiv(int dividend, int divisor, RoundingMode mode) { try { // Using divide(int p, int q, // RoundingMode mode) // method of Guava's IntMath class int quotient = IntMath.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[]) { int dividend = 32; int divisor = 0; try { // Function calling findDiv(dividend, divisor, RoundingMode.HALF_EVEN); } catch (Exception e) { System.out.println(e); } } } Output: java.lang.ArithmeticException: / by zero Reference : https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#divide-int-int-java.math.RoundingMode- Comment More infoAdvertise with us Next Article Java Guava | IntMath.divide(int, int, RoundingMode) method with Examples S Sahil_Bansall Follow Improve Article Tags : Java java-guava Guava-Functions Guava-IntMath Practice Tags : Java Similar Reads 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 3 min read Java Guava | IntMath log10(int x, RoundingMode mode) method with Examples The log10(int x, RoundingMode mode) method of Guavaâs IntMath Class accepts two parameters and calculates the base-10 logarithmic value of the first parameter rounded according to the rounding mode specified by the second parameter. Syntax: public static int log10(int x, RoundingMode mode) Parameter 3 min read Java Guava | IntMath.checkedAdd(int a, int b) method with Examples The checkedAdd(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their sum. Syntax: public static int checkedAdd(int a, int b) Parameters: The method accepts two int values a and b and computes their sum. Return Value: The method returns the sum of 2 min read Java Guava | IntMath.checkedPow(int b, int k) method with Examples checkedPow(int b, int k) is a method of Guava's IntMath Class which accepts two parameters b and k and is used to find the k-th power of b. Syntax: public static int checkedPow(int b, int k) Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised to the 2 min read Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples The checkedMultiply(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their product. Syntax: public static int checkedMultiply(int a, int b) Parameters: The method accepts two int values a and b and computes their product. Return Value: The method r 2 min read Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples The checkedSubtract(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their difference. Syntax: public static int checkedSubtract(int a, int b) Parameters: The method accepts two int values a and b and computes their difference. Return Value: The me 2 min read Java Guava | sqrt(int x, RoundingMode mode) method of IntMath Class The method sqrt(int x, RoundingMode mode) of Guava's IntMath class returns the square root of x, rounded with the specified rounding mode. Syntax: public static int sqrt(int x, RoundingMode mode) Exceptions: IllegalArgumentException: if x < 0. ArithmeticException: if mode is RoundingMode.UNNECESS 3 min read Java Guava | factorial(int n) method of IntMath Class with Examples The factorial(int n) method of Guava's IntMath Class returns the product of the first n positive integers, which is n!. Syntax: public static int factorial(int n) Parameter: The method accepts only one parameter n which is of integer type and is to be used to find the factorial. Return Value: This m 2 min read Java Guava | LongMath log10(long x, RoundingMode mode) method with Examples The method log10(long x, RoundingMode mode) of Guavaâs LongMath Class accepts two parameters and calculates the base-10 logarithmic value of the first parameter rounded according to the rounding mode specified by the second parameter. Syntax: public static int log10(long x, RoundingMode mode) Parame 3 min read Java Guava | log2(int x, RoundingMode mode) method of IntMath The log2(int x, RoundingMode mode) method of Guava's IntMath accepts two parameters and calculates the base-2 logarithmic value of the first parameter rounded according to the rounding mode specified by the second parameter. Syntax : public static int log2(int x, RoundingMode mode) Parameters : The 3 min read Like