BigDecimal longValueExact() Method in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// longValueExact() 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("267694723232");
        b2 = new BigDecimal("721111845617");
  
        // Displaying their respective Long Values
        System.out.println("Exact Long Value of "
        b1 + " is " + b1.longValueExact());
        System.out.println("Exact Long Value of "
        b2 + " is " + b2.longValueExact());
    }
}

chevron_right


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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// Arithmetic Exception occurrence
// in longValueExact() 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("267694723232435121868");
        b2 = new BigDecimal("72111184561789104423");
        // Displaying their respective Long Values
        // using longValue()
        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());
        // Exception handling
        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");
        }
    }
}

chevron_right


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()



My Personal Notes arrow_drop_up


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.