时隔一年,再次总结,分为两种情况:
1、.5结尾,向右取整
2、非.5结尾,取最接近的整数
java 中的 Math.round(-1.5) 等于多少?
等于 -1,因为在数轴上取值时,中间值(0.5)向右取整,所以正 0.5 是往上取整,负 0.5 是直接舍弃。
再结合该文章所述
https://blog.csdn.net/shruber/article/details/78413706
如果不对的话帮忙提出来
我总结了个结论,目前暂时没错
总结:分为两种情况
1.脑子里画出数轴图,然后向右取整,这对于 数字.5的有效
Math.round(1.5) = 2,
Math.round(-1.5) = -1
最主要就是向右取整
2.像Math.round(2.60f),这种怎么办
Math.round(2.60f) = 3
Math.round(-2.60f) = -3
这个时候如果按向右取整就错,这个时候应该是返回最接近它的整数,若有两个返回接近的整数,则取最大的那个,
Math.round(2.40f) = 2
Math.round(-2.40f) = -2