天天看點

Java保留小數點後*位數的方法

1.(double) (Math.round(sd3*10000)/10000.0); 

這樣為保持4位

(double) (Math.round(sd3*100)/100.0);

這樣為保持2位.

 2.另一種辦法

import java.text.DecimalFormat;

DecimalFormat df2  = new DecimalFormat("###.00");

DecimalFormat df2  = new DecimalFormat("###.000");

System.out.println(df2.format(doube_var));

第一個為2位,第二個為3位.