如何将 double
值转换为 int
执行以下操作:
Double If x = 4.97542. Convert to int x = 4.
Double If x = 4.23544. Convert to int x = 4.
也就是说,答案总是向下取整。
最佳答案
如果你明确地将cast double
转换为int
,小数部分将被截断。例如:
int x = (int) 4.97542; //gives 4 only
int x = (int) 4.23544; //gives 4 only
此外,您还可以使用 Math.floor()
方法对值进行四舍五入,以防您希望返回 double
值。
关于java - 将 double 转换为 Int,向下舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10280520/