What is Java math BigDecimal divide() Method? - Java.math Package

What is Java math BigDecimal divide() Method?

Description

The java.math.BigDecimal.divide(BigDecimal divisor, int scale, RoundingMode roundingMode) returns a BigDecimal whose price is (this / divisor), and whose scale is as specific. If rounding must be done to generate a result with the desired scale, the desired rounding mode is applied.

Declaration

Following is the declaration for java.math.BigDecimal.divide() technique.

Parameters

  • Divisor − Value by which this BigDecimal is to be divided.
  • Scale − Scale of the BigDecimal quotient to be returned.
  • Rounding Mode − Rounding mode to apply.

Return Value

This method returns a BigDecimal object whose value is this / divisor rounded as specified to given scale

Exception

Arithmetic Exception − If divisor is zero, roundingMode == RoundingMode.needless and the desired scale is insufficient to represent the result of the division precisely

Example

The following example suggests using math.BigDecimal.divide() technique.

Let us compile and run the above program, this will produce the following result −


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java.math Package Topics