- Java.math package classes
- Java.math - Home
- Java.math - BigDecimal
- Java.math - BigInteger
- Java.math - MathContext
- Java.math package extras
- Java.math - Enumerations
- Java.math - Discussion
Java.math.BigDecimal.divideAndRemainder() Method
Description
The java.math.BigDecimal.divideAndRemainder(BigDecimal divisor, MathContext mc) returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.
If both the integer quotient and remainder are needed, this method is faster than using the divideToIntegralValue and remainder methods separately because the division need only be carried out once.
Declaration
Following is the declaration for java.math.BigDecimal.divideAndRemainder() method.
public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc)
Parameters
divisor − Value by which this BigDecimal is to be divided, and the remainder computed.
mc − The context to use.
Return Value
This method returns a two element BigDecimal array: the quotient (the result of divideToIntegralValue) is the initial element and the remainder is the final element.
Exception
ArithmeticException − If divisor == 0
ArithmeticException − If the result is inexact but the rounding mode is UNNECESSARY, or mc.precision > 0 and the result of this.divideToIntgralValue(divisor) would require a precision of more than mc.precision digits.
Example
The following example shows the usage of math.BigDecimal.divideAndRemainder() method.
package com.tutorialspoint; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create 2 BigDecimal objects BigDecimal bg1, bg2; bg1 = new BigDecimal("143.145"); bg2 = new BigDecimal("10.01"); MathContext mc = new MathContext(2); // BigDecimal array bg stores result of bg1/bg2, // using mc BigDecimal bg[] = bg1.divideAndRemainder(bg2, mc); // print quotient and remainder System.out.println("Division result"); System.out.println("Quotient is " + bg[0] ); System.out.println("Remainder is " + bg[1] ); } }
Let us compile and run the above program, this will produce the following result −
Division result Quotient is 14 Remainder is 3.005