Math.round 理解

本文深入探讨Java中Math.round()方法的实现原理,通过示例代码展示了该方法如何处理正数和负数的四舍五入问题,并比较了其与Math.floor()函数的区别。揭示了round()方法在处理小数时的特殊规则,与常规的四舍五入有所不同。
 @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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值