The
java.math.BigDecimal.longValueExact() is an in-built function which converts
this BigDecimal to a long value as well as checks for lost information. This function throws 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 a long value.
Syntax:
public long longValueExact()
Parameters: This function does not accept any parameters.
Return Value: This function returns the long value of
this BigDecimal.
Exception: The function throws
ArithmeticException if there is a non-zero fractional part in
this BigDecimal or its value is too big to be represented as long.
Examples:
Input : "1987812456121"
Output : 1987812456121
Input : "721111"
Output : 721111
Below programs illustrate the use of java.math.BigDecimal.longValueExact() method:
Program 1:
Output:
Exact Long Value of 267694723232 is 267694723232
Exact Long Value of 721111845617 is 721111845617
Note: Unlike longValue() function which function discards any fractional part of
this BigDecimal and the returns only the lower-order 64 bits when result of the conversion is too big to be represented as a long value, this function throws
Arithmetic Exception in such occurrences.
Program 2: