Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaBigDecimal divideToIntegralValue() Method in Java with Examples

BigDecimal divideToIntegralValue() Method in Java with Examples

The java.math.BigDecimal.divideToIntegralValue(BigDecimal divisor) is used to calculate the integer part of the quotient of two BigDecimals (this / divisor) which is rounded down. The preferred scale of the result is (this.scale() – divisor.scale()). 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 divideToIntegralValue method available in Java which is listed below:

  • divideToIntegralValue(BigDecimal divisor)
  • divideToIntegralValue(BigDecimal divisor, MathContext mc)

divideToIntegralValue(BigDecimal divisor)

Syntax:

public BigDecimal divideToIntegralValue(BigDecimal divisor)

Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided. Return value: This method returns a BigDecimal which holds the integer part of result (this / divisor). Exception: The parameter divisor must not be 0 otherwise Arithmetic Exception is thrown. Below programs is used to illustrate the divideToIntegralValue() method of BigDecimal. 

Java




// Java program to demonstrate
// divideToIntegralValue() method of BigDecimal
 
import java.math.BigDecimal;
 
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
            = "31452678569";
        String input2
            = "2468";
 
        // Convert the string input to BigDecimal
        BigDecimal a
            = new BigDecimal(input1);
        BigDecimal divisor
            = new BigDecimal(input2);
 
        // Using divideToIntegralValue() method
        res = a.divideToIntegralValue(divisor);
 
        // Display the result in BigDecimal
        System.out.println(res);
    }
}


Output:

12744197

divideToIntegralValue(BigDecimal divisor, MathContext mc)

Since the integer part of the exact quotient does not depend on the rounding mode, the rounding mode does not affect the values returned by this method. The preferred scale of the result is (this.scale() – divisor.scale()). Syntax:

public BigDecimal divideToIntegralValue(BigDecimal divisor,
                                        MathContext mc)

Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided and a parameter mc of type MathContext for context settings. Return value: This method returns a BigDecimal which holds the integer part of result (this / divisor). Exception: The method throws Arithmetic Exception for following conditions:

  • The parameter divisor must not be 0.
  • If mc.precision > 0 and the result requires a precision of more than mc.precision digits.

Below programs is used to illustrate the divideToIntegralValue() method of BigDecimal. Example 1: 

Java




// Java program to demonstrate
// divideToIntegralValue() 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
            = "24536482";
        String input2
            = "2";
 
        // 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);
 
        // Using divideToIntegralValue() method
        res = a.divideToIntegralValue(divisor, mc);
 
        // Display the result in BigDecimal
        System.out.println(res);
    }
}


Output:

12268241

Example 2: Program illustrating the Exception occurred in divideToIntegralValue() method 

Java




// Java program to demonstrate
// divideToIntegralValue() 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
            = "24536482";
        String input2
            = "2";
 
        // 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);
 
        // As the result requires
        // a precision of more than
        // mc.precision digits
        // So Exception occur
        try {
 
            // Using divideToIntegralValue() method
            res = a.divideToIntegralValue(divisor, mc);
 
            // Display the result in BigDecimal
            System.out.println(res);
        }
        catch (ArithmeticException e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.ArithmeticException: Division impossible

References: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#divideToIntegralValue(java.math.BigDecimal)

RELATED ARTICLES

Most Popular

Recent Comments