Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A report shows totals that differ by one cent from the accounting system. The code computes Math.round(amount * 100) / 100.0 on double values, and accounting uses half-even rounding on decimal amounts. Explain the differences and fix the code.

There are two causes. A double cannot hold most decimal amounts exactly, so amount * 100 is often a hair below the true value, for example 1.015 * 100 is 101.49999999999999, and Math.round gives 101 instead of 102. And Math.round rounds exact halves up, while accounting uses half-even, so even exact values like 0.125 become 0.13 instead of 0.12. The fix is to keep amounts as BigDecimal from the original decimal text, round with setScale(2, RoundingMode.HALF_EVEN) at the same points accounting rounds, and test with edge values supplied by the accounting team.

Why this question is asked

A one-cent difference sounds trivial until it has to be explained to auditors, or until it appears on thousands of invoices. Reconciliation failures like this are common in reporting code because doubles are convenient and almost right. Interviewers ask it to see whether we can separate the two independent causes, binary representation and the rounding rule, and whether we know that where we round matters as much as how. In this answer, we will recap how doubles store decimals, reproduce both differences, compare the rounding modes, fix the code with BigDecimal, and decide where in the calculation rounding should happen.

1A Quick Recap of How Doubles Store Decimals

A double stores a number in binary: a sign, an exponent and 52 bits of fraction. Just as one third has no exact decimal form, most decimal fractions such as 0.1, 1.015 or 2.675 have no exact binary form. The double holds the nearest binary value instead, which may be a little above or a little below the decimal we wrote. Printing hides this, because Double.toString shows the shortest decimal that maps back to the same double.

Java
System.out.println(1.015); // 1.015: printing hides the error
System.out.println(new BigDecimal(1.015)); // 1.0149999999999999023003738329862244427204132080078125
System.out.println(1.015 * 100); // 101.49999999999999

new BigDecimal(double) shows the exact value the double holds, and it is slightly below 1.015. Multiplying by 100 keeps that error, so the product lands just below the half.

2Reproducing Both Differences

Now the report's formula against what accounting does with decimal amounts.

Java
static double report(double amount) { return Math.round(amount * 100) / 100.0; }
report(1.015); // 1.01: 101.49999999999999 rounds down
report(0.125); // 0.13: 12.5 is exact, and round sends halves up
report(-0.125); // -0.12: halves go toward positive infinity
new BigDecimal("1.015").setScale(2, RoundingMode.HALF_EVEN); // 1.02
new BigDecimal("0.125").setScale(2, RoundingMode.HALF_EVEN); // 0.12
new BigDecimal("-0.125").setScale(2, RoundingMode.HALF_EVEN); // -0.12
Why the report and accounting disagree
AmountReport (double, round)Accounting (HALF_EVEN)Cause
1.0151.011.02double is 1.01499..., below the half
0.1250.130.12exact half: up versus to even
0.1350.140.14both agree: 3 is odd, so even goes up
-0.125-0.12-0.12round sends halves toward +infinity
Two independent causes, one representation and one rule.

Notice that the two causes produce differences in opposite directions. For 1.015, the representation error pushes the value below the half, so the report rounds down to 1.01, while accounting sees an exact half, the digit before it is 1, which is odd, so half-even rounds to 1.02. For 0.125, the double is exact, so representation is not the problem; the rule is. Math.round rounds the half up to 0.13, while half-even keeps 2, which is even, and gives 0.12.

So fixing only one cause is not enough. Switching to BigDecimal but keeping HALF_UP would fix 1.015 and still disagree on 0.125. Keeping double but using rint would fix 0.125 and still disagree on 1.015.

3A Quick Look at Rounding Modes

BigDecimal's RoundingMode names the rule for the digit we drop. HALF_UP rounds halves away from zero, the school rule, so 0.125 becomes 0.13 and -0.125 becomes -0.13. HALF_EVEN rounds halves to whichever neighbour has an even last digit, so 0.125 becomes 0.12 and 0.135 becomes 0.14. Over many amounts, HALF_EVEN rounds up and down equally often, so totals do not drift upward, which is why accounting systems use it. HALF_DOWN, UP, DOWN, CEILING, FLOOR and UNNECESSARY also exist; UNNECESSARY throws if rounding would be needed, which is useful for asserting that a value is already in cents.

Math.round fits none of these exactly: it rounds halves toward positive infinity, so it behaves like HALF_UP for positive amounts and like HALF_DOWN for negative ones, which also means refunds and charges of the same size can round differently.

4The Fix: BigDecimal From the Original Text

The fix starts where the amount enters the system. If the amount comes from a database DECIMAL column, JDBC already gives us a BigDecimal through getBigDecimal, and JPA maps it to a BigDecimal field. If it comes from a file or JSON, we parse the text with new BigDecimal(text). Only if we are stuck with a double from an old API do we use BigDecimal.valueOf(double), which goes through Double.toString and therefore gives 1.015 rather than the long binary expansion.

Java
// Before
double total = 0;
for (Line l : lines) total += l.amount(); // doubles accumulate error
return Math.round(total * 100) / 100.0;
// After
BigDecimal total = BigDecimal.ZERO;
for (Line l : lines) {
total = total.add(l.amount()); // BigDecimal from the DECIMAL column
}
return total.setScale(2, RoundingMode.HALF_EVEN);
BigDecimal.valueOf(1.015).setScale(2, RoundingMode.HALF_EVEN); // 1.02: via the shortest decimal text
new BigDecimal(1.015).setScale(2, RoundingMode.HALF_EVEN); // 1.01: the exact binary value is below the half

Notice the last two lines. new BigDecimal(1.015) faithfully preserves the binary error and gives the wrong cent again, so it is the one constructor to avoid for money. BigDecimal is immutable, so add returns a new value that we must assign; forgetting that assignment is a common slip.

We also compare BigDecimals with compareTo, not equals, because equals also compares the scale, so 1.0 and 1.00 are not equal by equals.

5Round at the Same Points as Accounting

Even with exact arithmetic and the same rounding mode, the report can still differ if it rounds at different points. Rounding each invoice line to cents and then summing gives a different result from summing exact amounts and rounding the total. For example, three lines of 0.333 each sum to 0.999, which rounds to 1.00, but rounded per line they are 0.33 each and total 0.99.

Step throughMatching the accounting calculation
  1. Read amounts as BigDecimalDECIMAL column or decimal text
  2. Compute each line exactlyprice x quantity, tax
  3. Does accounting round per line?
    YessetScale(2, HALF_EVEN) per line, then sumNoSum exactly, then round the total
  4. Compare with accounting test casesx.xx5 values, negatives, many small lines

4 steps in order. Play them, or step through them one at a time.

Both the rounding rule and the rounding points must match the system of record.

So we ask the accounting team where they round, per line, per tax group or per invoice, and copy that exactly. Then we ask them for real edge cases: amounts ending in 5 in the third decimal, negative amounts such as refunds, and invoices with many small lines. These become unit tests that compare our report with their expected results, so any future change to the formula is caught before the next reconciliation.

6Follow-up questions

Can we keep doubles and add a small epsilon before rounding?

No. An epsilon like 1e-9 fixes some values and breaks others, it does not change the half-up rule, and it hides the problem from the next reader. Money needs decimal arithmetic, either BigDecimal or a long count of cents with explicit rules.

Is storing money as a long number of cents an alternative?

Yes, for many systems. A long of minor units is exact and fast. But multiplication by rates, tax percentages and currency conversion still produces fractions, and at those points we need a decimal type and an explicit rounding mode, so BigDecimal is still part of the solution.

How do you know which rounding mode the business uses?

We ask, and we write it down. HALF_EVEN is common in accounting, HALF_UP is common on customer invoices, and tax authorities sometimes prescribe their own rules. The rounding mode should be a named constant or configuration in one place, with a test referencing the rule's source.

7Conclusion

In this answer, we saw that the report differs from accounting for two independent reasons: doubles cannot hold amounts like 1.015 exactly, so multiplying by 100 lands below the half, and Math.round rounds halves up while accounting rounds half-even, so even exact halves like 0.125 differ. We fixed it by reading amounts as BigDecimal from the original decimal data, rounding with setScale(2, RoundingMode.HALF_EVEN), rounding at the same points accounting does, and testing with edge values from the accounting team.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two separate causes, and the fix has to address both. First, representation: a double cannot store most decimal amounts exactly, so 1.015 is really 1.01499 and so on, and multiplied by 100 it becomes 101.49999, which Math.round takes down to 101, so the report shows 1.01.

Then, if they want more

Second, the rounding rule: Math.round breaks ties toward positive infinity, while accounting uses half-even, so even with an exact value like 0.125 the report gives 0.13 and accounting gives 0.12. The fix is to stop using double for money.

I would read amounts as BigDecimal straight from the DECIMAL column or the original text, and if I am stuck with a double, use BigDecimal.valueOf rather than the double constructor, because new BigDecimal of a double keeps the binary error. Then round with setScale 2 and RoundingMode.HALF_EVEN.

I would also check where accounting rounds, per line or on the total, because rounding lines and then summing gives different results from rounding the sum, and copy their approach exactly.

Finally, I would ask the accounting team for real edge cases, amounts ending in 5 in the third decimal place, refunds and invoices with many small lines, and turn them into tests, so the report and the system of record stay in agreement.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A report shows totals that differ by one cent from the accounting system. The code...": https://scrivio-interview-library.netlify.app/java/types-and-values/b78976c5d2/