[英]Conversion from Long to Double in Java
Is there any way to convert a Long
data type to Double
or double
?有没有办法将Long
数据类型转换为Double
或double
?
For example, I need to convert 15552451L
to a double
data type.例如,我需要将15552451L
转换为double
数据类型。
You could simply do :你可以简单地做:
double d = (double)15552451L;
Or you could get double from Long object as :或者你可以从 Long 对象获得 double 为:
Long l = new Long(15552451L);
double d = l.doubleValue();
简单的铸造?
double d = (double)15552451L;
As already mentioned, you can simply cast long to double.如前所述,您可以简单地将 long 转换为 double。 But be careful with long to double conversion because long to double is a narrowing conversion in java.但是要小心long 到 double 的转换,因为 long 到 double 是 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.从 double 类型到 long 类型的转换需要从 64 位浮点值到 64 位整数表示的非平凡转换。Depending on the actual run-time value, information may be lost.根据实际运行时值,信息可能会丢失。
eg following program will print 1 not 0例如以下程序将打印 1 而不是 0
long number = 499999999000000001L;
double converted = (double) number;
System.out.println( number - (long) converted);
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.这将为您提供与long
文字具有相同位模式的double
精度值。
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) ) (见Double.longBitsToDouble(long) )
You also can get the long
back with你也可以得到long
回来
long bits = Double.doubleToRawLongBits(Double.NaN);
(See Double.doubleToRawLongBits(double) ) (见Double.doubleToRawLongBits(double) )
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.这样我们就可以毫无问题地将 Long 类型转换为 Double 或 Float 或 Int,因为将字符串值转换为 Double 或 Float 或 Int 很容易。
You can try something like this:你可以尝试这样的事情:
long x = somevalue;
double y = Double.longBitsToDouble(x);
What do you mean by a long date type?长日期类型是什么意思?
You can cast a long to a double:您可以将 long 转换为 double:
double d = (double) 15552451L;
I think it is good for you.我认为这对你有好处。
BigDecimal.valueOf([LONG_VALUE]).doubleValue()
How about this code?这段代码怎么样? :D :D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.