## Default Rounding Mode in Java

Lately I was a bit surprised when I found out that the default rounding mode in Java is something called “half even”. This API docs page provides good information about rounding in Java. For HAVE_EVEN it says:

Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it’s even. Note that this is the rounding mode that statistically minimizes cumulative error when applied repeatedly over a sequence of calculations. It is sometimes known as “Banker’s rounding,” and is chiefly used in the USA. This rounding mode is analogous to the rounding policy used for float and double arithmetic in Java.

An example makes it clear:

```
new java.text.DecimalFormat("0.00").format(23.515) -> result is "23.52"
new java.text.DecimalFormat("0.00").format(23.525) -> result is "23.52"
```

This was a bit confusing for me because in Austria the “default rounding mode” is “half up”. So to set the rounding mode to HALF_UP I had to use the method `setRoundingMode`

(which is available sind Java 1.6). In older version of Java you have to use `BigDecimal`

‘s `setScale`

method.