在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
类与 double
原语之间的差异>。另请注意, Double
是一个号
,所以它有方法 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
, when you can use to get an primitive int
.
这篇关于在Java中将双精度值转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文