Conversion from Long to Double in Java

前端 未结 8 445
隐瞒了意图╮
隐瞒了意图╮ 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:36

    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);
    

提交回复
热议问题