# BigIntegerMath divide() function | Guava | Java

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.

Syntax:

```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.

#### 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 illustrates the BigIntegerMath.divide() method:

Example 1:

 `// Java code to show implementation of ` `// divide(BigInteger p, BigInteger q, RoundingMode mode) ` `// method of Guava's BigIntegerMath class ` ` `  `import` `java.math.*; ` `import` `com.google.common.math.BigIntegerMath; ` ` `  `class` `GFG { ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``BigInteger dividend1 = BigInteger.valueOf(``55``); ` `        ``BigInteger divisor1 = BigInteger.valueOf(``10``); ` ` `  `        ``// Using divide() ` `        ``// method of Guava's BigIntegerMath class ` `        ``BigInteger ` `            ``quotient1 ` `            ``= BigIntegerMath ` `                  ``.divide(dividend1, ` `                          ``divisor1, ` `                          ``RoundingMode.HALF_DOWN); ` ` `  `        ``System.out.println(dividend1 + ``" divided by "` `                           ``+ divisor1 ` `                           ``+ ``" with HALF_DOWN rounding mode: "` `                           ``+ quotient1); ` ` `  `        ``BigInteger dividend2 = BigInteger.valueOf(``55``); ` `        ``BigInteger divisor2 = BigInteger.valueOf(``10``); ` ` `  `        ``// Using divide() ` `        ``// method of Guava's BigIntegerMath class ` `        ``BigInteger ` `            ``quotient2 ` `            ``= BigIntegerMath ` `                  ``.divide(dividend2, ` `                          ``divisor2, ` `                          ``RoundingMode.CEILING); ` ` `  `        ``System.out.println(dividend2 + ``" divided by "` `                           ``+ divisor2 ` `                           ``+ ``" with CEILING rounding mode: "` `                           ``+ quotient2); ` `    ``} ` `} `

Output:

```55 divided by 10 with HALF_DOWN rounding mode: 5
55 divided by 10 with CEILING rounding mode: 6
```

Example 2:

 `// Java code to show implementation of ` `// divide(BigInteger p, BigInteger q, RoundingMode mode) ` `// method of Guava's BigIntegerMath class ` ` `  `import` `java.math.*; ` `import` `com.google.common.math.BigIntegerMath; ` ` `  `class` `GFG { ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` ` `  `        ``try` `{ ` ` `  `            ``BigInteger dividend1 = BigInteger.valueOf(``25``); ` `            ``BigInteger divisor1 = BigInteger.valueOf(``0``); ` ` `  `            ``// Using divide() ` `            ``// method of Guava's BigIntegerMath class ` `            ``// This should raise "ArithmeticException" ` `            ``// as divisor1 = 0 ` `            ``BigInteger ` `                ``quotient1 ` `                ``= BigIntegerMath ` `                      ``.divide(dividend1, ` `                              ``divisor1, ` `                              ``RoundingMode.HALF_DOWN); ` `        ``} ` `        ``catch` `(Exception e) { ` `            ``System.out.println(``"Exception: "` `+ e); ` `        ``} ` `    ``} ` `} `

Output:

```Exception: java.lang.ArithmeticException: / by zero
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.