求助中。。

解决方案 »

  1.   

    1 浮点数永远是不准确的
    2 所谓取2位,只有在显示的时候才有效System.out.println(String.format("%.2f",1.333333555));
      

  2.   

    double a = 1.3333335555;
        
         BigDecimal bd = new BigDecimal(a);
        
         System.out.println(bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      

  3.   

    额,可以用Math.round或者floor等其他取整方法,先放大100倍,然后缩回来
      

  4.   

    public static void main(String[] args) {
    double a = 1.3333335555;
    String str = String.valueOf(a);
    int index = str.indexOf('.') + 1;
    str = str.substring(index, index + 2);
    System.out.println(str);
    }
      

  5.   

    这么做只是输出控制了 没返回一个double值吧
      

  6.   

    我的意思大家没有明白吧。

    Math.round(1.333333555*100)/100
    确实可以得到 1.33这个数字,不过如果你要继续使用浮点数类型的的话,依然是不准确的!
      

  7.   


    System.out.printf("%.2f", 1.3333335555);
      

  8.   

    http://blog.csdn.net/silentbalanceyh/archive/2009/09/28/4608360.aspx
    最开始的第一个小节我写了如何处理浮点数据的,可以参考一下
      

  9.   

    用double表示的浮点数本来就不是精确的
      

  10.   

    转换成string类型再取最后两位
      

  11.   

    System.out.println((int)(a*100)/100.0);我试了,可以的!!