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:
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.