# BigDecimal divide

In this tutorial we will see about BigDecimal‘s divide method. BigDecimal’s divide method is used to divide one Bigdecimal by another.You can specify scale and rounding mode to get the output according to your need. There are 6 overloaded version of divide method.

## Syntax

```BigDecimal divide ( BigDecimal  divisor)
BigDecimal divide ( BigDecimal  divisor, int roundingMode)
BigDecimal divide ( BigDecimal  divisor,int scale, int roundingMode)
BigDecimal divide ( BigDecimal  divisor, int scale,  RoundingMode  roundingMode)
BigDecimal divide ( BigDecimal  divisor,  MathContext  mc)
BigDecimal divide ( BigDecimal  divisor,  RoundingMode  roundingMode)```

## Return type

returns BigDecimal

## BigDecimal round example

Let’s understand BigDecimal’s divide method with the help of example

```import java.math.*;
package org.arpit.java2blog;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalDivideMain {

public static void main(String args[])
{
BigDecimal b1=new BigDecimal("18");
BigDecimal b2=new BigDecimal("5");
BigDecimal result=b1.divide(b2);
System.out.println("18/5 = "+result.toString());

// Using divide ( BigDecimal  divisor, int scale,  RoundingMode  roundingMode)
BigDecimal b3=new BigDecimal("17");
BigDecimal b4=new BigDecimal("3");
BigDecimal result1=b3.divide(b4,3,RoundingMode.HALF_UP);
System.out.println("17/3 = "+result1.toString());
}
}```

Above program will generate below output.

18/5 = 3.6
17/3 = 5.667

Please be careful when you use divide method.Please check if divisor is zero or not before dividing otherwise you will get divide by zero exception.

That’s all about BigDecimal’s divide method.

The post BigDecimal divide appeared first on Java2Blog.

This post first appeared on How To Learn Java Programming, please read the originial post: here

# Share the post

BigDecimal divide

×