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);
- 使用
版权声明
本文仅代表作者观点,不代表米安网络立场。
博豪信息




发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。