Conversion from Long to Double in Java
Solution 1
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
Simple casting?
double d = (double)15552451L;
Solution 3
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.
e.g. following program will print 1 not 0
long number = 499999999000000001L;
double converted = (double) number;
System.out.println( number - (long) converted);
Solution 4
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
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.
Related videos on Youtube
Harry
Updated on November 05, 2021Comments
-
Harry over 2 years
Is there any way to convert a
Long
data type toDouble
ordouble
?For example, I need to convert
15552451L
to adouble
data type. -
Jon Freedman over 13 yearsAnd if its a literal just
15552451d
-
Joe Kearney over 13 yearsDon't need any string parsing there:
Long.valueOf(15552451L).doubleValue()
if you want to go viaLong
. Note this is the same, internally, as the cast from adouble
, except that you're doing some auto/unboxing. -
crusam over 13 yearsI even think a cast is not necessary, you can create a double with a long value like this: "double d = 1234L;".
-
Jim Brissom over 13 yearsProbably not - but the question is basic enough to warrant the introduction of a concept like casting.
-
Joe Kearney over 13 yearsI did. Parsing a string to do this (now edited out of the answer) will degrade performance by many orders of magnitude. Boxing is less expensive but still worth avoiding, especially in cases like this (using
new
) where it can't be elided away. -
ams over 13 yearsI think the example was supposed to show that you can get from Long -> double. How the Long was initialised doesn't seem that important..
-
Joe Kearney over 13 yearsGranted the example in the answer looks more reasonable now having been edited. I maintain that conversion from a value to a double via a String is not good practice. I would have thought that to be fairly uncontroversial.
-
YoK over 13 yearsMy 2nd example is just showing how to get double value , if you have Long object. If you just had number , why would you assign long value to double by casting ? You could very well assign value directly as double.
-
YoK over 13 years@jjk If you look at question , it does mention converting from Long (object) to double. I don't understand what is your stand on this and what's the controversy.
-
Teemu Leisti almost 11 yearsI guess that a long date would be something like "Tuesday, 19 September 24893243092333".
-
Jiri Kremser almost 11 yearsThis does something else, see the jdoc (docs.oracle.com/javase/6/docs/api/java/lang/…)
-
pvorb almost 11 yearsYes. But he did not specify how he wants to convert
long
todouble
(I think casting is one of the first things you learn when using Java) nor did he accept any answer. Maybe he wants to get adouble
with the same bit pattern as thelong
value. Well, I don't know. So I just gave the answer. -
pvorb almost 11 yearsI added additional information, so the answer is more valuable.
-
cutemachine over 9 yearsYou do not need to cast explicitly. It just works:
double d = 15552451L;
-
abarisone almost 8 yearsIt's always better to add an explanation to the code you're posting as an answer, so that it will helps visitors understand why this is a good answer.
-
vpzomtrrfrt over 3 yearsYour link says the opposite, that double to long is a narrowing conversion, while long to double is widening
-
Antonio almost 2 yearsFormally, long->double is not narrowing, but this conversion can still loose precision: "A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4). " docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2
-
Antonio almost 2 years(continued)
(long) converted
is not a good illustration of the answer as double -> long is a narrowing conversion andnumber - (long) converted
should not always give 0 by definition docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3