VOOZH about

URL: https://www.geeksforgeeks.org/java/bigdecimal-divideandremainder-method-in-java-with-examples/

⇱ BigDecimal divideAndRemainder() Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

BigDecimal divideAndRemainder() Method in Java with Examples

Last Updated : 12 Jul, 2025
The java.math.BigDecimal.divideAndRemainder(BigDecimal divisor) is used to calculate both quotient and remainder of two BigDecimals. If both the integer quotient and remainder are needed then this method is faster than using the divideToIntegralValue() and remainder() methods separately because the division need only be carried out once. This method performs an operation upon the current BigDecimal by which this method is called and the BigDecimal passed as the parameter. There are two overloads of divideAndRemainder method available in Java which is listed below:
  • divideAndRemainder(BigDecimal divisor)
  • divideAndRemainder(BigDecimal divisor, MathContext mc)

divideAndRemainder(BigDecimal divisor)

Syntax:
public BigDecimal[] divideAndRemainder(BigDecimal divisor)
Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided for obtaining remainder and quotient. Return value: This method returns a BigDecimal array of size two, which holds the quotient and remainder. Exception: The parameter divisor must not be 0 otherwise Arithmetic Exception is thrown. Below programs is used to illustrate the divideAndRemainder() method of BigDecimal.
Output:
Quotient = 7002303
Remainder = 6334
Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divideToIntegralValue(java.math.BigDecimal)

divideAndRemainder(BigDecimal divisor, MathContext mc)

This method is used to calculate the quotient which is the result of divideToIntegralValue() followed by the result of remainder() on the two operands calculated with rounding according to the context settings. Syntax:
public BigDecimal[] divideAndRemainder(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 array of size two, which holds the quotient and remainder. Exception: The method throws Arithmetic Exception for following conditions:
  • If the parameter divisor is 0.
  • 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.
Below programs is used to illustrate the divideAndRemainder() method of BigDecimal. Program 1:
Output:
Quotient = 1783236
Remainder = 2023
Program 2: Program showing exception thrown by method divideAndRemainder().
Comment