Practical technical guides / Core IT: Java (java.text and floating point)

Java DecimalFormat Shows 4.125 as 4.12: HALF_EVEN by Default and Doubles That Are Not What They Print

DecimalFormat rounds HALF_EVEN and rounds the exact binary value, so 4.125 shows as 4.12 and 4.135 as 4.13 while String.format gives 4.13 and 4.14. Measured on JDK 23.

By Sandeep Belgavi · Published 2026-11-09

DecimalFormat rounds HALF_EVEN and rounds the exact binary value, so 4.125 shows as 4.12 and 4.135 as 4.13 while String.format gives 4.13 and 4.14. Measured on JDK 23.

A rating that showed 4.12

Belgavi's Book Store shows an average rating next to every book. A book with seven 4-star reviews and one 5-star review averages 33 / 8 = 4.125. The product page showed 4.12. The weekly report, which used String.format, showed 4.13. Tanishka, who writes the reports, filed it as a bug in one of them. Both were doing exactly what their documentation says, and they disagree for two separate reasons.

This guide measures both reasons on JDK 23.0.2 (the companion probe also compiles with --release 17), explains them from the JDK documentation, and ends with a short rule: round decimal values once, with an explicit RoundingMode, and never round a binary double when the decimal digits matter.

Reason one: DecimalFormat rounds HALF_EVEN by default

The java.text.DecimalFormat documentation is explicit: "By default, it uses RoundingMode.HALF_EVEN." Half-even, also called banker's rounding, rounds a tie to the neighbour whose last digit is even. 4.125 is exactly halfway between 4.12 and 4.13, and 2 is even, so it becomes 4.12. The same rule turns 0.125 into 0.12.

java.util.Formatter, behind String.format("%.2f", x), uses a different rule. Its documentation says that when the precision is less than the number of digits in Double.toString(x), "the value will be rounded using the round half up algorithm". So String.format gives 4.13.

Neither is wrong. They are two formatting APIs with two different defaults, and the default of DecimalFormat is the one fewer people expect.

Reason two: most decimal values are not what they print

4.125 happens to be exactly representable in binary (it is 33/8). Most two- or three-decimal values are not. The double nearest to 4.135 is 4.1349999999999997868371792719699442386627197265625. new BigDecimal(4.135) prints that exact value, which is a useful way to see it.

DecimalFormat rounds that exact binary value. Since JDK 8 (JDK-7131459, "DecimalFormat produces wrong format() results when close to a tie"), it takes the true value into account, so a value just below the tie rounds down under every half rounding mode. Formatter does something different: it starts from Double.toString(x), the shortest decimal string that identifies the double, which is "4.135", and rounds that string half up.

The measurements

The probe formats eight doubles to two decimals five ways, with Locale.ROOT so the output does not depend on the machine:

doubleexact binary value startsDecimalFormat defaultDecimalFormat HALF_UP%.2fBigDecimal.valueOf(x) HALF_UPMath.round(x*100)/100.0
4.1254.125 exactly4.124.134.134.134.13
4.1354.134999...4.134.134.144.144.14
4.1454.144999...4.144.144.154.154.14
0.1250.125 exactly0.120.130.130.130.13
1.0051.004999...1.001.001.011.011.0
2.6752.674999...2.672.672.682.682.68
1.1151.114999...1.111.111.121.121.12
8.3258.324999...8.328.328.338.338.32

Three things stand out.

Setting HALF_UP on DecimalFormat fixes the exact ties (4.125, 0.125) but not the near-ties (1.005, 2.675). For those, the binary value is below the tie, so every half rule rounds down. Switching rounding modes cannot fix a value that is not the one you meant.

String.format and BigDecimal.valueOf(x).setScale(2, HALF_UP) agree with each other on every row, because both start from Double.toString. That often matches what people expect, but it is a property of the shortest-string conversion, not of decimal arithmetic. A value computed by arithmetic, rather than typed, can have a shortest string that is not the decimal you meant.

Math.round(x * 100) / 100.0 is the least predictable: the multiplication itself rounds. 1.005 * 100 is 100.49999999999999 as a double, so it rounds to 100 and prints 1.0, and 4.145 * 100 rounds down while 4.135 * 100 rounds up.

Round decimals, once, explicitly

When the digits matter, keep the value in decimal from the start and choose the rounding mode in code:

int[] ratings = {5, 4, 4, 4, 4, 4, 4, 4};
int sum = Arrays.stream(ratings).sum();
BigDecimal average = new BigDecimal(sum)
    .divide(BigDecimal.valueOf(ratings.length), 3, RoundingMode.UNNECESSARY);  // 4.125, exact
BigDecimal shown = average.setScale(2, RoundingMode.HALF_UP);                 // 4.13

RoundingMode.UNNECESSARY throws if the division is not exact at that scale, which is a useful check when you believe it is. For averages that are not exact, divide with an explicit scale and mode, then round once for display. If values arrive as text (from a form, a CSV file or a JSON string), use new BigDecimal(String); new BigDecimal(double) gives you the binary value with all its digits.

When you do format with DecimalFormat, make both the rounding and the symbols explicit:

DecimalFormat twoPlaces = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.ROOT));
twoPlaces.setRoundingMode(RoundingMode.HALF_UP);

Without explicit symbols, the decimal separator follows the default locale; the probe prints 4,13 with German symbols. And remember that DecimalFormat is not thread-safe: create one per use or per thread rather than sharing a static instance.

Keep decimals decimal from end to end

Rounding correctly at the edge does not help if the value became a double somewhere in the middle. Three places where that usually happens:

  • The database. A numeric column read with ResultSet.getDouble is converted to binary on the way in. Read it with getBigDecimal and the scale is preserved.
  • JSON. Jackson deserialises a number such as 4.135 into a double unless the target field is BigDecimal or DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS is enabled for untyped values.
  • Arithmetic helpers. A utility that takes double parameters "for convenience" converts every caller's BigDecimal back to binary. Give such helpers BigDecimal signatures, or keep them for values where the last digit does not matter.

A useful test is to search the code path for every double between the source of a decimal value and the screen that shows it. Each one is a place where 4.135 can become 4.1349999999999997868.

Where HALF_EVEN is the right default

Half-even exists for a reason. When many rounded values are summed, half-up pushes every tie the same direction and the total drifts upward; half-even pushes half the ties each way and the bias cancels. For accumulated measurements and statistical outputs that is often what you want. The point is not that HALF_EVEN is wrong, it is that a display rule should be chosen deliberately and written in the code, so that two reports of the same number agree.

Failure modes

  • Two APIs, one number. A page and a report that format the same double with DecimalFormat and String.format will disagree on ties and near-ties. Format through one shared function.
  • Rounding twice. Rounding to three places and then to two can change the result (4.1449 to 4.145 to 4.15). Round once, at the edge.
  • Comparing doubles after rounding. Tests that assert format(x).equals("1.01") encode the binary accident. Test with decimal inputs and expected decimal outputs.
  • Locale surprises. Without Locale.ROOT or an explicit locale, machine settings change the separator and sometimes the digits.

When doubles are fine

For values whose last digit nobody reads exactly, such as a chart axis or a latency in milliseconds, doubles and any rounding mode are fine. The trouble starts when a displayed number is compared, reconciled, or read by a person who will notice that two screens disagree.

Verification

Run java src/RoundingProbe.java with any JDK 17 or newer and compare the table with the recording. The probe prints the JDK version, the default rounding mode of DecimalFormat and the exact binary value of each input, so a difference on another JDK shows exactly where the behaviour changed.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/java/decimal_rounding_probe. Views are my own and do not represent any employer, past or present.