Conversion from Long to Double in Java

前端 未结 8 514
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 01:07

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

For example, I need to convert 15552451L to a

8条回答
  •  没有蜡笔的小新
    2021-01-31 01:41

    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 long literal.

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

    double nan = Double.longBitsToDouble(0xfff0000000000001L);
    double positiveInfinity = Double.longBitsToDouble(0x7ff0000000000000L);
    double positiveInfinity = 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))

提交回复
热议问题