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:
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
BigDecimal b1, b2;
b1 = new BigDecimal( "267694723232" );
b2 = new BigDecimal( "721111845617" );
System.out.println( "Exact Long Value of " +
b1 + " is " + b1.longValueExact());
System.out.println( "Exact Long Value of " +
b2 + " is " + b2.longValueExact());
}
}
|
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:
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
BigDecimal b1, b2;
b1 = new BigDecimal( "267694723232435121868" );
b2 = new BigDecimal( "72111184561789104423" );
System.out.println( "Output by longValue() Function" );
System.out.println( "The Long Value of " + b1 + " is " + b1.longValue());
System.out.println( "The Long Value of " + b2 + " is " + b2.longValue());
System.out.println( "\nOutput by longValueExact() Function" );
try {
System.out.println( "Exact Long Value of " +
b1 + " is " + b1.longValueExact());
System.out.println( "Exact Long Value of " +
b2 + " is " + b2.longValueExact());
}
catch (ArithmeticException e) {
System.out.println( "Arithmetic Exception caught" );
}
}
}
|
Output:
Output by longValue() Function
The Long Value of 267694723232435121868 is -9006437873208152372
The Long Value of 72111184561789104423 is -1675791733049102041
Output by longValueExact() Function
Arithmetic Exception caught
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#longValueExact()
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
31 Dec, 2019
Like Article
Save Article