@Test
public void test1() {
System.out.println(Math.round(3.015*100));
System.out.println(Math.round(301.5));
System.out.println(Math.round(4.015*100));
System.out.println(Math.round(401.5));
System.out.println(Math.round(301.5)/100);
System.out.println(Math.round(3.5));
System.out.println(Math.round(3.015*100)/100.0);
System.out.println(round(3.015));
System.out.println(Math.round(4.015*100)/100.0);
System.out.println(round(4.015));
}
public double round(double value){   
        return Math.round(value*100)/100.0;   
}

解决方案 »

  1.   

    junit中测试结果302
    302
    401
    402
    3
    4
    3.02
    3.02
    4.01
    4.01
      

  2.   

            System.out.println(Math.round(4.015*100));
            System.out.println(Math.round(401.5));感觉和运算方式有关,4.015*100可能是double类型,401.5按照float类型计算。个人感觉,说得不对别拍砖。其他的没发现什么问题。
      

  3.   

    这是一个普遍的问题,是由于计算机计算精度的问题引起的
    System.out.println((4.015 * 100));
    输出
    401.49999999999994
      

  4.   

    改成这样就可以了
    System.out.println(Math.round(4.015f * 100));
      

  5.   


     double   abc   =   4.015;   //4.025 
            abc = new  java.math.BigDecimal(Double.toString(abc)).setScale(2,java.math.BigDecimal.ROUND_HALF_UP).doubleValue(); 
            System.out.println(abc*100);