关于JAVA取整

先转一篇文章,这篇文章讲得非常全面和详细,值得参考!

文章源地址:http://tech.e800.com.cn/articles/2009/85/1249435622799_1.html

舍掉小数取整:Math.floor(2.0)=2
舍掉小数取整:Math.floor(2.1)=2
舍掉小数取整:Math.floor(2.5)=2
舍掉小数取整:Math.floor(2.9)=2

舍掉小数取整:Math.floor(-2.0)=-2
舍掉小数取整:Math.floor(-2.1)=-3
舍掉小数取整:Math.floor(-2.5)=-3
舍掉小数取整:Math.floor(-2.9)=-3

四舍五入取整:Math.rint(2.0)=2
四舍五入取整:Math.rint(2.1)=2
四舍五入取整:Math.rint(2.5)=2
四舍五入取整:Math.rint(2.9)=3

四舍五入取整:Math.rint(-2.0)=-2
四舍五入取整:Math.rint(-2.1)=-2
四舍五入取整:Math.rint(-2.5)=-2
四舍五入取整:Math.rint(-2.9)=-3

凑整:Math.ceil(2.0)=2
凑整:Math.ceil(2.1)=3
凑整:Math.ceil(2.5)=3
凑整:Math.ceil(2.9)=3

凑整:Math.ceil(-2.0)=-2
凑整:Math.ceil(-2.1)=-2
凑整:Math.ceil(-2.5)=-2
凑整:Math.ceil(-2.9)=-2

四舍五入取整:Math.round(2.0)=2
四舍五入取整:Math.round(2.1)=2
四舍五入取整:Math.round(2.5)=3
四舍五入取整:Math.round(2.9)=3

四舍五入取整:Math.round(-2.0)=-2
四舍五入取整:Math.round(-2.1)=-2
四舍五入取整:Math.round(-2.5)=-2
四舍五入取整:Math.round(-2.9)=-3


我今天被Math.floor阴到的是,它对负数的取整。以前用过Math.floor取整,所以今天需要时也就随手一写。但是到后面跑的时候,发现在出现对负数处理的情况时,计算的结果总是会有所偏颇。在后台打出log看,发现对负数的处理总是出现莫名其妙的错误,都会有点小问题。看了好几遍代码,还是感觉没问题,如果有问题就只能是Math.floor的问题。于是看了下API,发现API写着:

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.

然后在网上一搜,就看到上面的那个文章,就全明白了...也给大家提个醒,加油!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值