java取小数点后两位

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



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



  class   Test1{  
      public   static   void   main(String[]   args){  
          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   =   13.4324;  
  d=((int)(d*100))/100;  

猜你喜欢

转载自mxdxm.iteye.com/blog/968116