开门见山: System.out.println(Math.round(-2.3));
System.out.println(Math.round(-2.7));
System.out.println(Math.round(-2.5));
输出结果是:-2
-3
-2
请高人解释,想听听高人的思路和想法,谢回复。

解决方案 »

  1.   

    看下API就知道了 round方法是返回:(long)Math.floor(a + 0.5d)
    而floor方法返回是一个最大的小于等于参数的数,也就是最接近的小于等于参数的数
      

  2.   

    round是四舍五入,注意负数5是舍的,例如:Math.round(1.5)值是2,Math.round(-1.5)值是-1
      

  3.   

    public static long round(double a)
    返回最接近参数的 long。结果将舍入为整数:加上 1/2,
    对结果调用 floor 并将所得结果强制转换为 long 类型。换句话说,结果等于以下表达式的值: (long)Math.floor(a + 0.5d)特殊情况如下: 如果参数为 NaN,那么结果为 0。 
    如果参数为负无穷大或任何小于等于 Long.MIN_VALUE 的值,那么结果等于 Long.MIN_VALUE 的值。 
    如果参数为正无穷大或任何大于等于 Long.MAX_VALUE 的值,那么结果等于 Long.MAX_VALUE 的值。参数:
    a - 舍入为 long 的浮点值。 
    返回:
    舍入为最接近的 long 值的参数值。
    怎么了?  四舍五入啊
      

  4.   

    该方法的doc已经说的很明确了:
    Returns the closest int 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 int. In other words, the result is equal to the value of the expression: (int)Math.floor(a + 0.5f)
      

  5.   

    对于任意的X >= 0
    都有,
    Math.round(-X) = -1 * Math.round(X)