代码如下:
public static void main(String[] args) {
System.out.println("向上取整:" + (int) Math.ceil(96.1));// 97 (去掉小数凑整:不管小数是多少,都进一)
System.out.println("向下取整:" + (int) Math.floor(96.8));// 96 (去掉小数凑整:不论小数是多少,都不进位)
System.out.println("四舍五入取整:" + Math.round(96.1));// 96 (这个好理解,不解释)
System.out.println("四舍五入取整:" + Math.round(96.8));// 97
}
结果:
向上取整:97
向下取整:96
四舍五入取整:96
四舍五入取整:97
这篇博客介绍了Java中进行数值运算的几个关键方法,包括使用Math.ceil()进行向上取整,Math.floor()进行向下取整,以及Math.round()进行四舍五入取整的示例。通过具体的代码演示了这些方法在处理浮点数时的行为,帮助读者理解它们的区别和应用。
5502

被折叠的 条评论
为什么被折叠?



