java中的Math.round()函数 | 您所在的位置:网站首页 › round四舍五入函数怎么用 › java中的Math.round()函数 |
一、
我们在idea中通过Math对round()函数进行调用的时候,可以看到: 如果round()函数中传入float型的参数,则得到int型的返回值; 如果round()函数中传入double型的参数,则得到long型的返回值; 二、对于round(double a):long的函数Returns the closest long to the argument, with ties rounding to positive infinity(返回的是最接近参数的一个long 值) 常规情况下 例: Math.round(11.6) = 12;Math.round(-11.6) = -12; Math.round(-0.1) = 0;Math.round(0.1) = 0; 如果出现向上向下距离一样的数值 例: Math.round(11.5) ,首先与 11.5最接近的有两个整数 11 和 12,取较大的那个,结果就是12; Math.round(-11.5),首先与 -11.5最接近的有两个整数 -11 和 -12,取较大的那个,结果就是-11; 三个特例 如果参数为 NaN(无穷与非数值),那么结果为 0。 如果参数为负无穷大或任何小于等于 Long.MIN_VALUE 的值,那么结果等于Long.MIN_VALUE 的值。 如果参数为正无穷大或任何大于等于 Long.MAX_VALUE 的值,那么结果等于Long.MAX_VALUE 的值。 double l = Long.MIN_VALUE; System.out.println(l); // -9.223372036854776E18 System.out.println(Math.round(l)); // -9223372036854775808 System.out.println(Math.round(l - 100000)); // -9223372036854775808 l = Long.MAX_VALUE; System.out.println(l); // 9.223372036854776E18 System.out.println(Math.round(l)); // 9223372036854775807 System.out.println(Math.round(l + 100000)); // 9223372036854775807 l = Double.NaN; System.out.println(Math.round(l)); // 0 三、本文参考以下文章原文链接 四、扩展内容有趣的NaN类型 |
CopyRight 2018-2019 实验室设备网 版权所有 |