我编写的这段代码将 double
转换为 int
出现异常。
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot cast from Double to int
这是我的代码
Double d = 10.9;
int i = (int)(d);
最佳答案
Double
是原始 double
之上的包装器 类。它可以转换为double
,但不能直接转换为int
。
如果您使用 double
而不是 Double
,它将编译:
double d = 10.9;
int i = (int)(d);
你也可以在中间加一个double
的转换,像这样:
int i = (int)((double)d);
关于java - Double 未转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088522/