@Test
public void MathRound(){
System.out.println(Math.round(-1.5)+" > "+(Math.round(-1.5)==Math.floor(-1.0))); //-1
System.out.println(Math.round(-1.2)+" > "+(Math.round(-1.2)==Math.floor(-0.7))); //-1
System.out.println(Math.round(1.5)+" > "+(Math.round(1.5)==Math.floor(2))); //2
System.out.println(Math.round(1.2)+" > "+(Math.round(1.2)==Math.floor(1.7))); //1
System.out.println(Math.round(0.8)+" > "+(Math.round(0.8)==Math.floor(1.3))); //1
//加0.5 调用floor 函数 floor(a+0.5) floor 是楼层的意思,有向下取整,-1.0 就是-1层,-0.7就是地板往下0.7米就是-1层了
//1.7 就是一楼往上0.7米是1楼,1.3往上0.3米还是一楼.SB 老外写的函数 与大中华的四舍五入 有点不一样。
}
加0.5 调用floor 函数 floor(a+0.5) floor 是楼层的意思,有向下取整,-1.0 就是-1层,-0.7就是地板往下0.7米就是-1层了
1.7 就是一楼往上0.7米是1楼,1.3往上0.3米还是一楼.SB 老外写的函数 与大中华的四舍五入 有点不一样。
运行结果:
-1 > true
-1 > true
2 > true
1 > true
1 > true
本文深入探讨Java中Math.round()方法的实现原理,通过示例代码展示了该方法如何处理正数和负数的四舍五入问题,并比较了其与Math.floor()函数的区别。揭示了round()方法在处理小数时的特殊规则,与常规的四舍五入有所不同。
9万+

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



