代码:

public static void main(String[] args) {

double d1=1.7894247;
double d2=0.987456321;
     DecimalFormat  df=new  DecimalFormat("#0.00");
   System.out.println(df.format(d1));
   System.out.println(df.format(d2));
   
   
   double   f   =   111231.5585;  
   BigDecimal   b   =   new   BigDecimal(f);  
   double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
   System.out.println("f1 :"+f1);
 
double d3 = 3.1415926;
String d4 = String .format(String.valueOf(d3),"%.2f");
System.out.println(d4);

}



输出:


1.79
0.99


f1 :111231.56

3.1415926


本文转载:CSDN博客