Conversion from Long to Double in Java

251,643

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.

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

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.

Share:
251,643

Related videos on Youtube

Harry
Author by

Harry

Updated on November 05, 2021

Comments

  • Harry
    Harry over 2 years

    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.

  • Jon Freedman
    Jon Freedman over 13 years
    And if its a literal just 15552451d
  • Joe Kearney
    Joe Kearney over 13 years
    Don't need any string parsing there: Long.valueOf(15552451L).doubleValue() if you want to go via Long. Note this is the same, internally, as the cast from a double, except that you're doing some auto/unboxing.
  • crusam
    crusam over 13 years
    I even think a cast is not necessary, you can create a double with a long value like this: "double d = 1234L;".
  • Jim Brissom
    Jim Brissom over 13 years
    Probably not - but the question is basic enough to warrant the introduction of a concept like casting.
  • Joe Kearney
    Joe Kearney over 13 years
    I 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
    ams over 13 years
    I 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
    Joe Kearney over 13 years
    Granted 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
    YoK over 13 years
    My 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
    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
    Teemu Leisti almost 11 years
    I guess that a long date would be something like "Tuesday, 19 September 24893243092333".
  • Jiri Kremser
    Jiri Kremser almost 11 years
    This does something else, see the jdoc (docs.oracle.com/javase/6/docs/api/java/lang/…)
  • pvorb
    pvorb almost 11 years
    Yes. But he did not specify how he wants to convert long to double (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 a double with the same bit pattern as the long value. Well, I don't know. So I just gave the answer.
  • pvorb
    pvorb almost 11 years
    I added additional information, so the answer is more valuable.
  • cutemachine
    cutemachine over 9 years
    You do not need to cast explicitly. It just works: double d = 15552451L;
  • abarisone
    abarisone almost 8 years
    It'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
    vpzomtrrfrt over 3 years
    Your link says the opposite, that double to long is a narrowing conversion, while long to double is widening
  • Antonio
    Antonio almost 2 years
    Formally, 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
    Antonio almost 2 years
    (continued) (long) converted is not a good illustration of the answer as double -> long is a narrowing conversion and number - (long) converted should not always give 0 by definition docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1‌​.3