BigDecimal intvalueExact() Method in Java
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()); } } |
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 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()
Recommended Posts:
- BigInteger intValueExact() Method in Java with Examples
- BigDecimal ulp() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal plus() method in Java
- BigDecimal min() Method in Java
- BigDecimal max() Method in Java
- BigDecimal byteValueExact() Method in Java
- BigDecimal pow() method in Java with Examples
- BigDecimal valueOf() Method in Java
- BigDecimal stripTrailingZeros() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal scaleByPowerOfTen() method in Java
- BigDecimal round() Method in Java
- BigDecimal toBigIntegerExact() Method in Java
- BigDecimal add() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


