Conversion from Long to Double in Java

JavaType Conversion

Java Problem Overview


Is there any way to convert a Long data type to Double or double?

For example, I need to convert 15552451L to a double data type.

Java Solutions


Solution 1 - Java

You could simply do :

double d = (double)15552451L;

Or you could get double from Long object as :

Long l = new Long(15552451L);
double d = l.doubleValue();

Solution 2 - Java

Simple casting?

double d = (double)15552451L;

Solution 3 - Java

As already mentioned, you can simply cast long to double. But be careful with long to double conversion because long to double is a narrowing conversion in java.

Conversion from type double to type long requires a nontrivial translation from a 64-bit floating-point value to the 64-bit integer representation. Depending on the actual run-time value, information may be lost.

e.g. following program will print 1 not 0

    long number = 499999999000000001L;
	double converted = (double) number;
	System.out.println( number - (long) converted);

Solution 4 - Java

Are you looking for the binary conversion?

double result = Double.longBitsToDouble(15552451L);

This will give you the double with the same bit pattern as the provided long.

Binary or hexadecimal literals will come in handy, here. Here are some examples.

double nan = Double.longBitsToDouble(0xfff0000000000001L);
double positiveInfinity = Double.longBitsToDouble(0x7ff0000000000000L);
double negativeInfinity = Double.longBitsToDouble(0xfff0000000000000L);

(See Double.longBitsToDouble(long))

You also can get the long back with

long bits = Double.doubleToRawLongBits(Double.NaN);

(See Double.doubleToRawLongBits(double))

Solution 5 - Java

Long i = 1000000;
String s = i + "";
Double d = Double.parseDouble(s);
Float f = Float.parseFloat(s);

This way we can convert Long type to Double or Float or Int without any problem because it's easy to convert string value to Double or Float or Int.

Solution 6 - Java

What do you mean by a long date type?

You can cast a long to a double:

double d = (double) 15552451L;

Solution 7 - Java

You can try something like this:

long x = somevalue;

double y = Double.longBitsToDouble(x);

Solution 8 - Java

I think it is good for you.

BigDecimal.valueOf([LONG_VALUE]).doubleValue()

How about this code? :D

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHarryView Question on Stackoverflow
Solution 1 - JavaYoKView Answer on Stackoverflow
Solution 2 - JavaJim BrissomView Answer on Stackoverflow
Solution 3 - JavaDeepView Answer on Stackoverflow
Solution 4 - JavapvorbView Answer on Stackoverflow
Solution 5 - JavaManav PatadiaView Answer on Stackoverflow
Solution 6 - JavaJoe KearneyView Answer on Stackoverflow
Solution 7 - JavaVikasView Answer on Stackoverflow
Solution 8 - JavaspearkkkView Answer on Stackoverflow