【轉載學習】From http://www.w3china.org/blog/more.asp?name=hongrui&id=40621
JAVA除法保留小數點後兩位的方法
原創空間, 文章收藏, 網上資源邢紅瑞 發表於 2008-10-14 19:19:45
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位.
3 新的jdk
String.format("%10.2f%%", doube_var);