The
java.math.BigDecimal.divide(BigDecimal divisor) is used to calculate the
Quotient of two BigDecimals. The
Quotient is given by
(this / divisor). This method performs an operation upon the current BigDecimal by which this method is called and the BigDecimal passed as the parameter.
There are five overloads of divide method available in Java which is listed below:
- divide(BigDecimal divisor)
- divide(BigDecimal divisor, MathContext mc)
- divide(BigDecimal divisor, RoundingMode roundingMode)
- divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
- divide(BigDecimal divisor, int roundingMode)
Note: The method
divide(BigDecimal divisor, int roundingMode) is deprecated since
Java 9.
divide(BigDecimal divisor)
The
Quotient is given by
(this / divisor) and whose preferred scale is (this.
scale() - divisor.
scale()).
Syntax:
public BigDecimal divide(BigDecimal divisor)
Parameters: This method accepts a parameter
divisor by which this BigDecimal is to be divided for obtaining quotient.
Return value: This method returns a BigDecimal which holds the result
(this / divisor).
Exception: If parameter
divisor is
0 or the exact quotient does not have a terminating decimal expansion then
Arithmetic Exception is thrown.
Below programs is used to illustrate the divide() method of BigDecimal.
Output:
800000
Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divide(java.math.BigDecimal)
divide(BigDecimal divisor, MathContext mc)
This method is used to calculate the quotient of two BigDecimals whose value is
(this / divisor), with rounding according to the context settings.
Syntax:
public BigDecimal divide(BigDecimal divisor,
MathContext mc)
Parameters: This method accepts two parameters:
- divisor by which this BigDecimal is to be divided
- mc of type MathContext for context settings.
Return value: This method returns a BigDecimal which holds the result
(this / divisor) and rounded as necessary.
Exception: The method throws
Arithmetic Exception if the result is inexact but the rounding mode is UNNECESSARY or mc.precision == 0 and the quotient has a non-terminating decimal expansion.
Below programs is used to illustrate the divide() method of BigDecimal.
Output:
92941
Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divide(java.math.BigDecimal, java.math.MathContext)
divide(BigDecimal divisor, RoundingMode roundingMode)
The
Quotient is given by
(this / divisor) and whose preferred scale is this.
scale(). The specific rounding mode is applied if rounding is needed to generate result with the given scale.
Syntax:
public BigDecimal divide(BigDecimal divisor,
RoundingMode roundingMode)
Parameters: This method accepts two parameters:
- divisor by which this BigDecimal is to be divided
- roundingMode of type RoundingMode that tells which rounding mode to apply.
Return value: This method returns a BigDecimal which holds the result
(this / divisor).
Exception: The method throws
Arithmetic Exception if roundingMode is UNNECESSARY and this.scale() is insufficient to represent the result of the division exactly or
Divisor is 0.
Below programs is used to illustrate the divide() method of BigDecimal.
Output:
9294122934
Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divide(java.math.BigDecimal, java.math.RoundingMode)
divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
The
Quotient is given by
(this / divisor) whose preferred scale is specified. If rounding is required to generate a result with the specified scale, the specified rounding mode is applied.
Syntax:
public BigDecimal divide(BigDecimal divisor,
int scale,
RoundingMode roundingMode)
Parameters: This method accepts three parameters:
- divisor by which this BigDecimal is to be divided
- roundingMode of type RoundingMode that tells which rounding mode to apply
- scale which sets the scale of the quotient.
Return value: This method returns a BigDecimal which holds the result
(this / divisor).
Exception: The method throws
Arithmetic Exception if roundingMode is UNNECESSARY and the specified scale is insufficient to represent the result of the division exactly or
Divisor is 0.
Below programs is used to illustrate the divide() method of BigDecimal.