java 小数点取2位并且四舍五入

BigDecimal bd = new BigDecimal(0.0199999999999);  
System.out.println("res:"+bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  

 

 

         DecimalFormat df = new DecimalFormat("#.00");   
         System.out.println(df.format(Double.parseDouble(str)));   
         System.out.println("=================");  
         System.out.println(String.format("%.2f", Double.parseDouble(str)));  
         System.out.println("=================");  
         NumberFormat nf = NumberFormat.getNumberInstance();   
         nf.setMaximumFractionDigits(2);   
         System.out.println(nf.format(Double.parseDouble(str)));  

 

posted @ 2016-05-23 14:43  狂奔的小狮子  阅读(1119)  评论(0编辑  收藏  举报