Thursday, March 19, 2009

Rounding strategy used by DecimalFormat?

Decimal format uses BigDecimal .ROUND_HALF_EVEN and this cannot be changed.

If you need an alternate rounding strategy then you need to use BigDecimal to do your formatting.

double d = 123.45;
BigDecimal bd = new BigDecimal(d);
bd.setScale(1, BigDecimal.ROUND_HALF_UP);
String formatted = bd.toString();

No comments: