Open In App

BigDecimal intvalueExact() Method in Java

Last Updated : 04 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.math.BigDecimal.intValueExact() is an inbuilt function which converts this BigDecimal to an integer value as well as checks for the lost information. This function throws an Arithmetic Exception if there is any fractional part of this BigDecimal or if the result of the conversion is too big to be represented as an integer value.

Syntax:

public int intValueExact()

Parameters: This function accepts no parameters.

Return Value: This function returns the integer value of this BigDecimal.

Exception: The function throws an ArithmeticException if there is a non-zero fractional part in this BigDecimal or its value is too big to be represented as an integer.

Examples:

Input : "19878124"
Output : 19878124

Input : "721111"
Output : 721111

Below programs illustrate the use of java.math.BigDecimal.intValueExact() method:
Program 1:




// Java program to illustrate
// intValueExact() method
import java.math.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating 2 BigDecimal Objects
        BigDecimal b1, b2;
        // Assigning values to b1, b2
        b1 = new BigDecimal("19878124");
        b2 = new BigDecimal("721111");
        // Displaying their respective Integer Values
        System.out.println("Exact Integer Value of " +
        b1 + " is " + b1.intValueExact());
        System.out.println("Exact Integer Value of " +
        b2 + " is " + b2.intValueExact());
    }
}


Output:

Exact Integer Value of 19878124 is 19878124
Exact Integer Value of 721111 is 721111

Note: Unlike intValue() function which function discards any fractional part of this BigDecimal and returns only the lower-order 32 bits when result of the conversion is too big to be represented as a an integer value, this function throws Arithmetic Exception in such occurrences.

Program 2: This program will illustrate when this function throws Exception.




// Java program to illustrate
// Arithmetic Exception occurrence
// in intValueExact() method
import java.math.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Creating 2 BigDecimal Objects
        BigDecimal b1, b2;
  
        // Assigning values to b1, b2
        b1 = new BigDecimal("3232435121868179");
        b2 = new BigDecimal("84561789104423214");
  
        // Displaying their respective Integer Values
        // using intValue()
        System.out.println("Output by intValue() Function");
        System.out.println("The Integer Value of "
        b1 + " is " + b1.intValue());
        System.out.println("The Integer Value of "
        b2 + " is " + b2.intValue());
          
        // Exception handling
        System.out.println("\nOutput by intValueExact() Function");
        try {
            System.out.println("Exact Integer Value of "
            b1 + " is " + b1.intValueExact());
            System.out.println("Exact Integer Value of "
            b2 + " is " + b2.intValueExact());
        }
        catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception caught");
        }
    }
}


Output:

Output by intValue() Function
The Integer Value of 3232435121868179 is -214774381
The Integer Value of 84561789104423214 is -920387282

Output by intValueExact() Function
Arithmetic Exception caught

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#intValueExact()



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads