Java取小数点后两位(N位)的方法归纳

java.text.DecimalFormat df = new java.text.DecimalFormat("#0.##");   
double d=3.14159;   
System.out.println(df.format(d));

java.math.BigDecimal bd = new java.math.BigDecimal("3.14159265");   
bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);

class Test{   
      public static void main(String se77en[]){   
          double ret = convert(3.14159);   
          System.out.println(ret);   
      }   
        
      static double convert(double value){   
          long l1 = Math.round(value*100);   //四舍五入   
          double ret = l1/100.0;    //注意:使用100.0而不是100   
          return ret;   
      }   
  }

double d = 3.14159;   
d = (double)((int)(d*100+0.5))/100;

第五种
double f = 1.1111;
    Double d = Double.valueOf(String.format("%.02f", f));
    System.out.println(d);
 

猜你喜欢

转载自52xianmengyu.iteye.com/blog/1672974