如何直接将 Double 转换为 int?

也许这是个愚蠢的问题。我想去掉 Double数的小数部分。但我做不到。它显示不兼容类型的错误。怎么办?

Double to int转换成一行... ... 请帮忙,谢谢

404936 次浏览

try casting the value

double d = 1.2345;
long l = (long) d;

If you really should use Double instead of double you even can get the int Value of Double by calling:

Double d = new Double(1.23);
int i = d.intValue();

Else its already described by Peter Lawreys answer.

All other answer are correct, but remember that if you cast double to int you will loss decimal value.. so 2.9 double become 2 int.

You can use Math.round(double) function or simply do :

(int)(yourDoubleValue + 0.5d)
int average_in_int = ( (Double) Math.ceil( sum/count ) ).intValue();
double myDb = 12.3;
int myInt = (int) myDb;

Result is: myInt = 12