我有代码:
double dd=2.99;
int ii=(int)(dd);//ii==2
我想要 ii 中的 3 个。有什么快速简单的方法吗?
最佳答案
你想要Math.round()
或者 - 如果您总是想向上舍入 - Math.ceil()
。同样,有一个 Math.floor()
总是向下舍入。
非常挑剔,您应该注意 Math.round( double )
返回 long
,因此如果您想存储结果,可能会损失精度在 int
中(对于非常大的 double
)。
关于java - 如何将顶部的 double 转换为 int 舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20633058/