Convert long to double in Java

Mohammad Irfan Jan 30, 2023 Nov 04, 2021
  1. Convert long to double Using Implicit Casting in Java
  2. Convert long to double Using Explicit Casting in Java
  3. Convert long to double Using the doubleValue() Method in Java
  4. Convert long to double Using the longBitsToDouble() Method in Java
  5. Convert long to double Using the longBitsToDouble() Method in Java
Convert long to double in Java

This tutorial introduces how to convert long type to double type in Java.

In Java, long and double are both used to store numerical values. The long is used to store non-floating values, whereas the double is used to store floating-point values. Both take the same number of bytes (16 bytes) to store data into memory.

In this article, we will learn to convert long type value to double type by using some methods such as doubleValue(), longBitsToDouble(), and parseDouble() method. Let’s start with some examples.

Convert long to double Using Implicit Casting in Java

In this example, we are converting a long type value to a double type. Since both use the same bytes, conversion gets implemented easily, and double values append a floating-point after the long value. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		long l = 97;
		System.out.println("long value: "+l);
		double d = l;
		System.out.println("double value: "+d); 
	}
}

Output:

long value: 97
double value: 97.0

Convert long to double Using Explicit Casting in Java

Although we don’t need to use explicit casting for long to double conversion for better code, we can use explicit casting. See the example below; we get the same result as we get in the above example.

public class SimpleTesting{
	public static void main(String[] args){
		long l = 97;
		System.out.println("long value: "+l);
		double d = (double)l;
		System.out.println("double value: "+d); 
	}
}

Output:

long value: 97
double value: 97.0

Convert long to double Using the doubleValue() Method in Java

If you have a long object, you can simply use the doubleValue() method of the Long class to get a double type value. This method does not take any argument but returns a double after converting a long value. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		Long l = new Long(97);
		System.out.println("long value: "+l);
		double d = l.doubleValue();
		System.out.println("double value: "+d); 
	}
}

Output:

long value: 97
double value: 97.0

Convert long to double Using the longBitsToDouble() Method in Java

We can use the longBitsToDouble() method as well to get double value from a long type. This is a static method and belongs to the Double class. This method actually does the binary level conversion. So, to get results in readable form, use the doubleToRawLongBits() method. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		long l = 97;
		System.out.println("long value: "+l);
		double d = Double.longBitsToDouble(l);
		System.out.println("double value: "+d);
		System.out.println(Double.doubleToRawLongBits(d));
	}
}

Output:

long value: 97
double value: 4.8E-322
97

Convert long to double Using the longBitsToDouble() Method in Java

The parseDouble() method takes a string type long object value and returns a double value. This is a static method of the Double class and can only be used for string arguments. See the example below.

public class SimpleTesting{
	public static void main(String[] args){
		Long l = new Long(97);
		System.out.println("long value: "+l);
		double d = Double.parseDouble(l+"");
		System.out.println("double value: "+d);
	}
}

Output:

long value: 97
double value: 97.0

Related Article - Java Long

Related Article - Java Double