在 Java 中将双精度转换为整数 [英] Cast Double to Integer in Java
本文介绍了在 Java 中将双精度转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以将 java.lang.Double
转换为 java.lang.Integer
?
Any way to cast java.lang.Double
to java.lang.Integer
?
抛出异常
"java.lang.ClassCastException: java.lang.Double 与 java.lang.Integer 不兼容"
"java.lang.ClassCastException: java.lang.Double incompatible with java.lang.Integer"
推荐答案
A Double
不是 Integer
,因此转换不起作用.请注意 Double
class 和 double
primitive 之间的区别.另请注意,Double
是一个 Number
,所以它有方法 intValue
,你可以用它来获取作为原始值的 int代码>.
A Double
is not an Integer
, so the cast won't work. Note the difference between the Double
class and the double
primitive. Also note that a Double
is a Number
, so it has the method intValue
, which you can use to get the value as a primitive int
.
这篇关于在 Java 中将双精度转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文