Quick revision
The short answer
First we decide, with the business, whether tax is rounded per line or once on the total, because the two give different results and rules or regulations often require one of them. Then we fix the scale to the currency's minor units from Currency.getDefaultFractionDigits, pick one RoundingMode such as HALF_EVEN or HALF_UP as required, keep extra precision in intermediate steps and round only at the defined points. BigDecimal makes this exact but is slower than long minor units, and whatever we choose, the scale must be stored with the value in the database as NUMERIC(p,s) and stated in the API contract.
What to remember
In this answer, we designed a rounding policy for invoices. We saw that rounding tax per line or on the total gives different results, 0.48 versus 0.49 in our example, so the business or regulation decides. We fixed the final scale from Currency.getDefaultFractionDigits, chose one RoundingMode as a named policy, kept intermediate results at higher precision and rounded only at defined points, weighed BigDecimal's clarity against long's speed, and carried the scale into NUMERIC(p,s) columns and API contracts.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we decide, with the business, whether tax is rounded per line or once on the total, because the two give different results and rules or regulations often require one of them. Then we fix the scale to the currency's minor units from Currency.getDefaultFractionDigits, pick one RoundingMode such as HALF_EVEN or HALF_UP as required, keep extra precision in intermediate steps and round only at the defined points. BigDecimal makes this exact but is slower than long minor units, and whatever we choose, the scale must be stored with the value in the database as NUMERIC(p,s) and stated in the API contract.
Why this question is asked
Invoices are where rounding stops being a technical detail. A customer adds up the lines and compares with the total, a tax authority checks the tax amount, and an accounting system reconciles with the payment. If the service rounds at different points from the rules, or differently in two places, those numbers disagree by a cent, and someone has to explain why. Interviewers ask this design question to see whether we know which choices exist, which of them are business decisions, and how to implement them precisely with BigDecimal. In this answer, we will recap scale and rounding modes, compare per-line and total rounding with real numbers, choose the scale and mode, decide where intermediate precision lives, weigh BigDecimal against long, and finish with storage and API contracts.
1A Quick Recap: Scale and RoundingMode
A BigDecimal has a value and a scale, the number of digits after the decimal point. setScale(2, mode) returns the value with exactly two decimals, rounding if necessary, and the RoundingMode says how to treat the dropped digits. HALF_UP rounds a tie (exactly half) away from zero, which is what most people learned at school. HALF_EVEN rounds a tie to the neighbour whose last digit is even, which is called banker's rounding and avoids a systematic upward bias when many values are rounded.
2Per-Line or on the Total: The Results Differ
Take an invoice with three lines of 1.99 each and a tax rate of 8.25 percent. Rounding the tax on each line and adding the rounded amounts gives a different tax than computing tax on the total and rounding once.
| Step | Round per line | Round on the total |
|---|---|---|
| Tax per line, exact | 0.164175 x 3 | not computed |
| Rounded tax per line | 0.16 x 3 | not computed |
| Net total | 5.97 | 5.97 |
| Tax on total, exact | not used | 0.492525 |
| Tax on the invoice | 0.48 | 0.49 |
Per-line rounding gives 0.48 and total rounding gives 0.49. Neither is wrong in itself; they are two different rules. Per-line rounding makes each line self-consistent, which matters when lines are shown with their own tax or refunded individually. Total rounding is closer to the exact value. Many tax regimes and business contracts specify one of them, so this is not a choice for the engineer to make alone: we ask finance or the tax team, and we write the answer into the design.
3Choosing the Scale and the Rounding Mode
The final scale comes from the currency, not from a constant 2. java.util.Currency knows the number of minor-unit digits for each ISO 4217 currency: 2 for EUR and USD, 0 for JPY, 3 for currencies such as KWD and BHD. Using it means the same code works for every market we sell in.
For the mode, we choose one and use it everywhere unless a rule says otherwise. HALF_EVEN is a good default for financial calculations because ties do not always go up. HALF_UP is required by some tax rules and is what customers expect when they check by hand. The important thing is that the mode is a named part of the policy, defined once, and never chosen ad hoc at a call site.
4Intermediate Precision and Where to Round
The second rule is to round as few times as possible, at defined points. Intermediate results, such as unit price times quantity, discounts as percentages, or currency conversion rates, are kept at full precision or at a generous fixed scale, for example 6 or 10 decimals, so that errors do not accumulate. Rounding happens only at the points the policy names, such as the tax per line, the line total and the invoice total.
- Unit price x quantityexact, no rounding
- Apply discount percentagekeep full precision
- Round line net amountcurrency scale, policy mode
- Tax per line or on total?YesRound each line's tax, then addNoAdd nets, compute tax once, round
- Invoice total = nets + taxalready at currency scale
5 steps in order. Play them, or step through them one at a time.
5BigDecimal or long Minor Units?
BigDecimal makes the decimal rules explicit and exact, but every operation allocates a new immutable object and is much slower than primitive arithmetic. For a service that computes a few thousand invoices a minute, that cost is negligible and the clarity is worth it. For very high volumes, such as pricing millions of items per second, long minor units are much faster, and adding amounts in cents is exact. Percentages and rates still need care with long, usually by doing the multiplication in BigDecimal or with integer arithmetic at a higher scale and then rounding once. A common compromise is long cents for storage and addition, with BigDecimal used only inside tax and discount calculations.
6Storage and API Contracts
The scale must survive outside the service. In the database we use NUMERIC(p,s), for example NUMERIC(19,4) for amounts that may carry intermediate precision or NUMERIC(19,2) for final amounts in two-decimal currencies, so the database neither rounds silently nor accepts more digits than we intend. In the API, amounts are sent as decimal strings or integer minor units together with the currency code, and the contract states the scale, so a consumer does not round again with its own rule. Finally, the policy is tested with real examples from finance, including ties, zero-decimal currencies and refunds, so a change in the code that alters a cent fails a test.
7Follow-up questions
How do we split a rounded total across lines so they add up?
With an allocation method that distributes the leftover cents. We round each share down, compute how many cents remain, and give one extra cent to the shares with the largest remainders. That way the parts always add up exactly to the total, which matters for refunds and split payments.
Should we store the unrounded tax too?
Often yes, at a higher scale, in an audit column. It explains how the rounded figure was reached, which helps during reconciliations and tax audits, and it lets us recompute if a rounding rule was applied incorrectly.
Why not round at every step to keep numbers tidy?
Because each rounding adds an error of up to half a minor unit, and over many steps those errors accumulate and can bias the result in one direction. Rounding only at the defined points keeps the result as close as possible to the exact value and makes it reproducible.
8Conclusion
In this answer, we designed a rounding policy for invoices. We saw that rounding tax per line or on the total gives different results, 0.48 versus 0.49 in our example, so the business or regulation decides. We fixed the final scale from Currency.getDefaultFractionDigits, chose one RoundingMode as a named policy, kept intermediate results at higher precision and rounded only at defined points, weighed BigDecimal's clarity against long's speed, and carried the scale into NUMERIC(p,s) columns and API contracts.
Open with
The first decision is not technical: is tax rounded per line or once on the invoice total? They give different results. For example, three lines of 1.99 at 8.25 percent give 0.48 if I round each line's tax and 0.49 if I round tax on the 5.97 total.
Then, if they want more
Tax rules and contracts often specify one, so I would get that from finance and write it into the design. Then I fix the scale. The final scale comes from the currency's minor units, using Currency.getDefaultFractionDigits, so EUR has 2, JPY 0 and KWD 3.
I choose one rounding mode as a named part of the policy, HALF_EVEN by default because ties do not all go up, or HALF_UP where a rule requires it, and never choose it ad hoc in the code.
Intermediate results like price times quantity, discounts and rates stay at full or high precision, and I round only at the defined points: line amounts, tax, and the total.
BigDecimal makes this exact and readable; it is immutable and slower than long, so at very high volumes I might store and add long minor units and use BigDecimal only inside the tax calculation.
Finally, the scale has to live outside the code too: NUMERIC(p,s) columns in the database, and in the API amounts as strings or minor units with the currency and a documented scale, with tests using real examples from finance.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Design the rounding policy for an invoicing service that computes tax per line and a...": https://scrivio-interview-library.netlify.app/java/types-and-values/8e358f3572/