Java:Math.round(-8.5) 等于 -8
可是
Excel:Round(-8.5, 0) 等于 -9
SQLServer:select round(-8.5, 0) 输出 -9
Oracle:select round(-8.5) from dual 输出 -9我觉得对负数四舍五入时,可以这样理解(假设num为正数):
Round(-num) = -Round(num),比如:Round(-8.5) = -9
但为什么Java等于-8?是Java的Bug吗?从数学的角度来说Round(-8.5)应该等于几?另外,还有个奇怪的问题:
double num=0.01+0.105;
System.out.println(new java.text.DecimalFormat("0.00").format(num)); //输出0.11
System.out.println(new java.text.DecimalFormat("0.00").format(0.115)); //输出0.12
为什么结果会不同呢?
可是
Excel:Round(-8.5, 0) 等于 -9
SQLServer:select round(-8.5, 0) 输出 -9
Oracle:select round(-8.5) from dual 输出 -9我觉得对负数四舍五入时,可以这样理解(假设num为正数):
Round(-num) = -Round(num),比如:Round(-8.5) = -9
但为什么Java等于-8?是Java的Bug吗?从数学的角度来说Round(-8.5)应该等于几?另外,还有个奇怪的问题:
double num=0.01+0.105;
System.out.println(new java.text.DecimalFormat("0.00").format(num)); //输出0.11
System.out.println(new java.text.DecimalFormat("0.00").format(0.115)); //输出0.12
为什么结果会不同呢?
全部显示
全部隐藏
返回某个数字按指定位数取整后的数字。语法ROUND(number,num_digits)Number 需要进行四舍五入的数字。Num_digits 指定的位数,按此位数进行四舍五入。说明如果 num_digits 大于 0,则四舍五入到指定的小数位。
如果 num_digits 等于 0,则四舍五入到最接近的整数。
如果 num_digits 小于 0,则在小数点左侧进行四舍五入。
public static long round(double a)Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression: (long)Math.floor(a + 0.5d)Special cases: If the argument is NaN, the result is 0.
If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.Parameters:
a - a floating-point value to be rounded to a long.
Returns:
the value of the argument rounded to the nearest long value.
See Also:
Long.MAX_VALUE, Long.MIN_VALUE
// TODO Auto-generated method stub
System.out.println(-8.1 + " " + Math.round(-8.1));
System.out.println(-8.2 + " " + Math.round(-8.2));
System.out.println(-8.3 + " " + Math.round(-8.3));
System.out.println(-8.4 + " " + Math.round(-8.4));
System.out.println(-8.5 + " " + Math.round(-8.5));
System.out.println(-8.6 + " " + Math.round(-8.6));
System.out.println(-8.7 + " " + Math.round(-8.7));
System.out.println(-8.8 + " " + Math.round(-8.8));
System.out.println(-8.9 + " " + Math.round(-8.9));
}运行结果:-8.1 -8
-8.2 -8
-8.3 -8
-8.4 -8
-8.5 -8
-8.6 -9
-8.7 -9
-8.8 -9
-8.9 -9
BUILD SUCCESSFUL (total time: 1 second)
按照java中对round()的运算法则(long)Math.floor(a + 0.5d),
当 -8.5<a<-8.0 如 a= -8.4
则带入公式中 返回是
(long)Math.floor(-8.4 + 0.5d)
=(long)Math.floor(-7.9)
=-8
这个运算结果是对的,
当 a=-8.5 返回 (long)Math.floor(-8.0)=-8
当 -9<a< -8.5 如a=-8.6 返回 (long)Math.floor(-8.1)=-9
这些结果从java中的运算逻辑看都是对的,至于和Excel中的运算结果不同应该是二者对round()方法有不同的定义
个人想法 ^_^!
我在ChinaJavaWorld上也发贴问了,有兴趣的朋友可以到这里看看http://bbs.chinajavaworld.com/post/view?bid=20&id=685584&sty=1&tpg=3&age=0
问题一:Excel、SQLServer、Oracle采用"away from zero"方式Round,而Java采用"towards positive infinity"方式Round。
问题二:是由于计算机存储空间有限,在进行十进制和二进制转换时截断了无穷的尾数。