四舍五入 四和五是指正的4,5
-11.5 这么看 -11.5 = -12 +0.5   ,0.5按四舍五入为1 ,-12+1 = -11,所以Math.round(-11.5)==-11
这是网上一个人对此方法的理解 我想听听别人的看法 因为API描述的很官方 导致我现在很混乱

解决方案 »

  1.   

    round
    public static int round(float a)返回最接近参数的 int。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 int 类型。换句话说,结果等于以下表达式的值: (int)Math.floor(a + 0.5f)怎么算都说明白了,怎么很"官方"呢?
      

  2.   

    按这个(int)Math.floor(a + 0.5f) 式子处理一下.
      

  3.   

    啊 看来我真正没理解的是floor方法。。 不好意思 我马上看看去
      

  4.   

    还有一道题说是:swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上? 答案: switch(expr1)中,expr1是一个整数表达式。因此传递给 switch 和 case 语句的参数应该是 int、 short、 char 或者 byte。long,string 都不能作用于swtich。 我觉得一句话搞定 switch内的参数就是int型的 只要是能转换成int类型的数据类型都可以 即int自身 short char byte 这样回答岂不是更好如有说错 一定指出
      

  5.   

    http://blog.chinajavaworld.com/entry/5127/0/ 就是这里面的
      

  6.   

    按书上的说法就是:
    switch (expression) {
        case n: statements
        case m: statements
        . . .
        default: statements
    }
    The expression must either be of an integer type (char, byte, short, or int, or a corresponding wrapper class) or an enum type.
      

  7.   

    啊 这是哪本书啊 这么权威 我都忘了枚举了 我一定要看看zang说一下书名吧 然后有没有中文版
      

  8.   

    The Java Programming Language
    第四版
    有中文版的.
      

  9.   

    有问题
    最开始那个问题API解释的意思完全不对了是不是应该改成这样public static int round(float a)返回最接近参数的 int。结果将参数舍入为整数,加上 1/2(0.5),对结果调用 floor 并将所得结果强制转换为 int 类型。少了一个词 逗号写成了冒号是不是这样
      

  10.   

    floor和ceil就用数学中的坐标来搞定ceil永远在返回那数靠近负坐标的数
    floor就返回靠近正坐标的数啦
      

  11.   

    java,Math得四舍五入是错误的
    现在有人还在用round?
    正确做法是
    public int getRound(double dSource){ 
        int iRound //BigDecimal的构造函数参数类型是
        double BigDecimal deSource = new BigDecimal(dSource); 
        //deSource.setScale(0,BigDecimal.ROUND_HALF_UP) 返回值类型 BigDecimal 
        //intValue() 方法将BigDecimal转化为int 
        iRound= deSource.setScale(0,BigDecimal.ROUND_HALF_UP).intValue(); 
        return iRound; 
    }
      

  12.   

    import java.math.BigDecimal;public class Test { public static double getRound(double dSource, int iRound){ 
    BigDecimal deSource = new BigDecimal(dSource); 
    return deSource.setScale(iRound, BigDecimal.ROUND_HALF_UP).doubleValue(); 
    }

    public static void main(String args[]) { System.out.println(getRound(-11.5555555, 0));
    System.out.println(getRound(-11.5555555, 1));
    System.out.println(getRound(-11.5555555, 2));
    }
    }
    修改后的代码
      

  13.   

    不管正数负数使用round都是加上正的0.5,然后取正就行了