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.
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.
Input : "19878124" Output : 19878124 Input : "721111" Output : 721111
Below programs illustrate the use of java.math.BigDecimal.intValueExact() method:
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.
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
- BigInteger intValueExact() Method in Java with Examples
- BigDecimal plus() method in Java
- BigDecimal ulp() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal min() Method in Java
- BigDecimal max() Method in Java
- BigDecimal hashCode() Method in Java
- BigDecimal longValue() Method in Java
- BigDecimal intValue() Method in Java
- BigDecimal precision() Method in Java
- BigDecimal pow() method in Java with Examples
- BigDecimal movePointLeft() Method in Java
- BigDecimal longValueExact() Method in Java
- BigDecimal round() Method in Java
- BigDecimal scale() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.