本文轉自 http://www.cnblogs.com/nayitian/p/3214178.html
1. 功能
將程序中的double值精確到小數點後兩位。可以四捨五入,也可以直接截斷。
比如:輸入12345.6789,輸出可以是12345.68也可以是12345.67。至於是否需要四捨五入,可以通過參數來決定(RoundingMode.UP/RoundingMode.DOWN等參數)。
2. 實現代碼
formatDouble1( ()Math.round(d*100)/100 formatDouble2( BigDecimal bg = BigDecimal(d).setScale(2 String formatDouble3(= nf.setMaximumFractionDigits(2 String formatDouble4(= DecimalFormat("#.00" String formatDouble5( String.format("%.2f" d = 12345.67890