Skip to content
Related Articles

Related Articles

Improve Article

Java Guava | IntMath.divide(int, int, RoundingMode) method with Examples

  • Last Updated : 01 Feb, 2019

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 ConstantDescription
CEILINGRounding mode to round towards positive infinity.
DOWNRounding mode to round towards zero.
FLOORRounding mode to round towards negative infinity.
HALF_DOWNRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
HALF_EVENRounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
HALF_UPRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
UPRounding mode to round away from zero.

Below examples illustrate the implementation of above method:

Example 1 :




// 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 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-

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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :