我想我所有的数据四舍五入后保存格式为两位小数,没有两位小数的用0显示,例如
3.125 显示为3.13
3.1   显示为3.10
能给段代码学习一下吗?

解决方案 »

  1.   

    1位小数就是Math.round(XX*10)/10   
        
    2位小数就是Math.round(XX*100)/100   
      

  2.   

    DecimalFormat dd = new java.text.DecimalFormat("#.00");String val = dd.format(3.12d);
    String val2 = dd.format(3.1d);
      

  3.   

    String out = String.format("%.2f",3.12);
    System.out.println(out);System.out.printf("%.2f",3.1);
      

  4.   


    //留小数点后两位数
    public static double leftTwoBit(double d){
    BigDecimal  b  =  new  BigDecimal(d);  
    double  d1  =  b.setScale(2,  BigDecimal.ROUND_HALF_UP).doubleValue();
    return d1;
    }