Detailed explanation of java BigDecimal divideAndRemainder() method

Before introducing this method, I want to talk about divideToIntegralValue and remainder methods:
divideToIntegralValue

**java.math.BigDecimal.divideToIntegralValue(BigDecimal divisor) **Returns a BigDecimal whose value is the integer part of the quotient (this/divisor) rounded off.

 

Let us compile and run the above program, which will produce the following result: 33.0
remainder

**java.math.BigDecimal.remainder(BigDecimal divisor) **The method returns a BigDecimal whose value is (this% divisor).
The remainder is given by this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)). This is not a modular operation, that is, its result may be negative.
The following example shows the usage of math.BigDecimal.remainder() method

 

 

Let us compile and run the above program, which will produce the following result:
3.54
divideAndRemainder

After introducing the above two methods, the next step is to introduce divideAndRemainder
java.math.BigDecimal.divideAndRemainder(BigDecimal divisor) returns a result containing divideToIntegralValue, and then the rest and the above are rounded according to the context setting to calculate the result of the two operands, the result BigDecimal array consisting of two elements.
If both integer quotients and remainders are needed, this method is faster than using divideToIntegralValue and remainder methods alone, because the division only needs to be performed once.
This method returns a BigDecimal array consisting of two elements: the quotient (the result of divideToIntegralValue) is the initial element, and the remainder is the final element.
The following example shows the usage of math.BigDecimal.divideAndRemainder() method

 

 

Let us compile and run the above program, which will produce the following result:
Division result
Quotient is 14.0
Remainder is 3.005

 

 

Guess you like

Origin blog.csdn.net/Crystalqy/article/details/89811604