LAST UPDATED: SEPTEMBER 3, 2020
Java Long doubleValue() Method
Java doubleValue()
method belongs to the Long
class. This method returns the double equivalent of the Long
after a widening primitive conversion(Conversion of a lower data type into a higher data type).
In short, this method is used to convert a Long object into a primitive double value.
Syntax:
public double doubleValue()
Parameter:
No parameter is passed in this method.
Returns:
It returns double value of the long object.
Example 1:
Here, using the doubleValue()
method, the long values are converted into its numerical double equivalent.
import java.lang.Long;
public class StudyTonight
{
public static void main(String[] args)
{
//converting Long object into double
Long x = 99L;
double i=x.doubleValue();
System.out.println(i);
Long y = 23L;
double d = y.doubleValue();
System.out.println(d);
}
}
99.0
23.0
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent double value.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value to be converted : ");
Scanner sc = new Scanner(System.in);
long i = sc.nextLong();
Long n = i ;
double val = n.doubleValue();
System.out.println("Double Value is: " + val);
}
catch(Exception e)
{
System.out.println("not a valid long");
}
}
}
Enter the value to be converted : 63
Double Value is: 63.0
**********************************************
Enter the value to be converted : -56
Double Value is: -56.0
**********************************************
Enter the value to be converted : 0x754
not a valid long
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.