The java.math.BigDecimal.longValue() is an in-built function which converts this BigDecimal to a long value. This function discards any fractional part of this BigDecimal. The function returns only the lower-order 64 bits when the result of the conversion is too big to be represented as a long value.
public long longValue()
Parameters: This function accepts no parameters.
Return Value: This function returns the long value of this BigDecimal.
Input : 1987812456121.176 Output : 1987812456121 Input : "721111" Output : 721111
Below programs illustrate the use of java.math.BigDecimal.longValue() method:
The Long Value of 1987812456121.176 is 1987812456121 The Long Value of 721111 is 721111
Note: Information regarding the overall magnitude and precision of large this BigDecimal values might be lost during the course of conversion by this function. As a consequence, a result with the opposite sign might be returned.
Program 2: Below program illustrates a scenario when the function returns a result with the opposite sign.
The Long Value of 267694723232435121868 is -9006437873208152372 The Long Value of 72111184561789104423 is -1675791733049102041
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.