Java中Math.round()方法原理解读

来源:互联网 发布:大量收购淘宝店铺 编辑:程序博客网 时间:2024/04/30 20:53

Java中Math.round()方法是将浮点数“四舍五入”转换成整数的方法,但这里的“四舍五入”和数学中的四舍五入有点差别,接下来就来解析一下在Java里的原理。
1、首先直接上测试代码:
public static void main(String[] args) {
System.out.println(Math.round(12.4));
System.out.println(Math.round(12.5));
System.out.println(Math.round(12.9));
System.out.println(Math.round(-12.4));
System.out.println(Math.round(-12.5));
System.out.println(Math.round(-12.9));
}
运行结果:
12
13
13
-12
-12
-13
在没有弄清原理之前,凭我自己猜想,我一直认为数字按照向0靠拢的思想,但结果表明我的猜想是错误的。
2、源码解析
看Math.round()内的实现,但JDK1.8内代码有点多(不知道其他版本的是怎样的),但其真正的意思也就是如下:
public static int round(float a) {
return (int)floor(a + 0.5f);
}

public static long round(double a) {
return (long)floor(a + 0.5d);
}
看到有两个重载的方法,一个接收float类型返回int类型,另一个接收double类型返回long类型,这也很好理解,float和int都是占4个字节,double和long占8个字节,这样精度就没有丢失。但平时使用的时候还是要注意,因为实际应用中最常用double和int,所以可能需要转换。
1)从上面的两个方法可以看出,都是对数字加0.5后在再floor()方法
2)floo()方法的原理:取小于等于参数的最大整数浮点数(可以理解为向下取整数浮点数),如floor(12.5)=12.0,floor(12.9)=12.0,floor(-12.5)=-13.0,floor(-12.9)=-13.0。
3、结论
Math中的round方法,求的值就是取小于等于(参数+0.5)的最大整数。

原创粉丝点击