关于Math.round()取整规则
Math.round()取整规则就是参数+0.5,然后向下取整,验证如下:
正数:
System.out.println(Math.round(1.49));//1
System.out.println(Math.round(1.5));//2
System.out.println(Math.round(1.99));//2
//------------------加0.5向下取整
System.out.println(Math.floor(1.49+0.5));//1.0
System.out.println(Math.floor(1.5+0.5));//2.0
System.out.println(Math.floor(1.99+0.5));//3.0
负数
System.out.println(Math.round(-1.49));//-1
System.out.println(Math.round(-1.5));//-1
System.out.println(Math.round(-1.99));//-2
//------------------加0.5向下取整
System.out.println(Math.floor(-1.49+0.5));//-1.0
System.out.println(Math.floor(-1.5+0.5));//-1.0
System.out.println(Math.floor(-1.99+0.5));//-2.0