Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaBigDecimal divideAndRemainder() Method in Java with Examples

BigDecimal divideAndRemainder() Method in Java with Examples

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.




// Java program to demonstrate
// divideAndRemainder() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res[];
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values
        String input1
            = "456865265569";
        String input2
            = "65245";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Using divideAndRemainder() method
        res = a.divideAndRemainder(divisor);
  
        // Display the result in BigDecimal
        System.out.println("Quotient = " + res[0]
                           + "\nRemainder = " + res[1]);
    }
}


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:




// Java program to demonstrate
// divideAndRemainder() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res[];
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values
        String input1
            = "4568652655";
        String input2
            = "2562";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Set precision to 10
        MathContext mc
            = new MathContext(10);
  
        try {
            // Using divideAndRemainder() method
            res = a.divideAndRemainder(divisor, mc);
  
            // Display the result in BigDecimal
            System.out.println("Quotient = " + res[0]
                               + "\nRemainder = " + res[1]);
        }
        catch (Exception e) {
  
            System.out.println(e);
        }
    }
}


Output:

Quotient = 1783236
Remainder = 2023

Program 2: Program showing exception thrown by method divideAndRemainder().




// Java program to demonstrate
// divideAndRemainder() method of BigDecimal
  
import java.math.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigDecimal object to store the result
        BigDecimal res[];
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values
        String input1
            = "4568652655";
        String input2
            = "2562";
  
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
  
        // Set precision to 5
        MathContext mc
            = new MathContext(5);
  
        try {
            // Using divideAndRemainder() method
            res = a.divideAndRemainder(divisor, mc);
  
            // Display the result in BigDecimal
            System.out.println("Quotient = " + res[0]
                               + "\nRemainder = "
                               + res[1]);
        }
        catch (Exception e) {
  
            System.out.println(e);
        }
    }
}


Last Updated :
17 Jun, 2019
Like Article
Save Article
Similar Reads
Related Tutorials
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments