Java Convert BigDecimal value into long value
In this Java core tutorial we learn how to convert a java.math.BigDecimal object to a long value in Java programming language.
How to convert BigDecimal to long in Java
In Java, with a given BigDecimal object we can use the java.math.BigDecimal.longValue() method to convert it to a long value as the following example Java code.
package simplesolution.dev; import java.math.BigDecimal; public class BigDecimalLongValueExamples public static void main(String. args) BigDecimal bigDecimal = new BigDecimal("999999999.01"); long longValue = bigDecimal.longValue(); System.out.println(bigDecimal); System.out.println(longValue); > >
Java.math.BigDecimal.longValue() Method
The java.math.BigDecimal.longValue()converts this BigDecimal to an long.This conversion is analogous to the narrowing primitive conversion from double to short. Any fractional part of this BigDecimal will be discarded, and if the resulting «BigInteger» is too big to fit in an long, only the low-order 64 bits are returned.
This conversion can lose information about the overall magnitude and precision of this BigDecimal value as well as return a result with the opposite sign.
Declaration
Following is the declaration for java.math.BigDecimal.longValue() method.
Specified by
longValue in class Number.
Parameters
Return Value
This method returns the long value of the BigDecimal Object.
Exception
Example
The following example shows the usage of math.BigDecimal.longValue() method.
package com.tutorialspoint; import java.math.*; public class BigDecimalDemo < public static void main(String[] args) < // create 2 BigDecimal objects BigDecimal bg1, bg2; // create 2 long objecs long l1,l2; bg1 = new BigDecimal("429.07"); bg2 = new BigDecimal("429496732223453626252"); // assign the long value of bg1 and bg2 to l1,l2 respectively l1 = bg1.longValue(); l2 = bg2.longValue(); String str1 = "long value of " + bg1 + " is " + l1; String str2 = "long value of " + bg2 + " is " + l2; // print l1,l2 values System.out.println( str1 ); System.out.println( str2 ); >>
Let us compile and run the above program, this will produce the following result −
long value of 429.07 is 429 long value of 429496732223453626252 is 5221618528133939084
BigDecimal convert
We can use the following methods to convert BigDecimal values to primitive data types.
- byte byteValueExact()
Converts this BigDecimal to a byte, checking for lost information. - double doubleValue()
Converts this BigDecimal to a double. - float floatValue()
Converts this BigDecimal to a float. - int intValue()
Converts this BigDecimal to an int. - int intValueExact()
Converts this BigDecimal to an int, checking for lost information. - long longValue()
Converts this BigDecimal to a long. - long longValueExact()
Converts this BigDecimal to a long, checking for lost information. - short shortValueExact()
Converts this BigDecimal to a short, checking for lost information. - BigInteger toBigInteger()
Converts this BigDecimal to a BigInteger. - BigInteger toBigIntegerExact()
Converts this BigDecimal to a BigInteger, checking for lost information.
Convert to float type value
floatValue() method is used in the following code to convert BigDecimal value to float type value.
import java.math.BigDecimal; /*j a v a2 s .co m*/ public class Main < public static void main(String[] args) < BigDecimal first = new BigDecimal(-1f); System.out.println(first.floatValue()); > >
Convert double and long to BigDecimal
- static BigDecimal valueOf(double val)
Translates a double into a BigDecimal. - static BigDecimal valueOf(long val)
Translates a long value into a BigDecimal with a scale of zero. - static BigDecimal valueOf(long unscaledVal, int scale)
Translates a long unscaled value and an int scale into a BigDecimal.
import java.math.BigDecimal; // j av a2s. co m public class Main < public static void main(String[] args) < System.out.println(BigDecimal.valueOf(1.00001)); >>
The code above generates the following result.
Next chapter.
What you will learn in the next chapter: