java中double转换为int怎么实现
在Java中将double
类型转换为int
类型可以使用强制类型转换或者使用Math
类提供的方法进行转换。
强制类型转换:
double d = 3.14;
int i = (int) d;
使用Math
类提供的方法:
使用Math.floor()
方法将double
向下取整转换为int
:
double d = 3.14;
int i = (int) Math.floor(d);
使用Math.ceil()
方法将double
向上取整转换为int
:
double d = 3.14;
int i = (int) Math.ceil(d);
使用Math.round()
方法将double
四舍五入转换为int
:
double d = 3.14;
int i = (int) Math.round(d);
阅读剩余
THE END