2014-09-29 149 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

相关问题