2014-09-29 181 views
4

我想使用逗號和2個小數點格式化BigDecimal數字。 例如Java格式帶逗號和2位小數點的BigDecimal數字

Amount is: 5.0001 and formatted to: 5.00 
Amount is: 999999999.999999 and formatted to: 999,999,999.99 
Amount is: 1000.4999 and formatted to: 1,000.49 
Amount is: 9999.089 and formatted to: 9,999.08 
Amount is: 0.19999 and formatted to: 0.19 
Amount is: 123456.99999999 and formatted to: 123,456.99 

回答

8

這應該足以獲得結果。

String.format("%,.2f", amount.setScale(2, RoundingMode.DOWN)); 
+2

輸出取決於區域設置,所以也許使用'格式(Locale.ENGLISH, 「%,. 2F」,amount.setScale(2,RoundingMode.DOWN))'。如果將區域設置更改爲「FRANCE」,則會看到幾乎不同的格式。 – Pshemo 2014-09-29 14:20:22

相關問題