Quick revision
The short answer
The standard is that money is never a double: it is a BigDecimal with a currency, or a long count of minor units, inside a Money type such as JSR 354's Moneta or an internal value class. Rounding uses the currency's scale and an explicit RoundingMode agreed with finance and tax law, applied only at defined steps, and the library designs out BigDecimal's pitfalls. Databases use NUMERIC columns, JSON carries amounts as strings, and the rollout uses a shared library, static analysis, reconciliation tests and a migration order based on financial exposure.
What to remember
In this answer, we set a standard for monetary arithmetic across 15 services. Money is a BigDecimal with a currency, or long minor units, inside a Money value type such as Moneta or an internal class, never a double. Rounding uses the currency's scale and an explicit mode agreed with finance and tax law, applied only at defined steps. The library hides BigDecimal's pitfalls with compareTo semantics, String or valueOf construction and divide with a scale and RoundingMode. Databases use NUMERIC columns and APIs send amounts as strings. The rollout uses a shared library, static analysis, reconciliation tests and a migration order based on financial exposure, with shadow comparisons before each switch.
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.
The standard is that money is never a double: it is a BigDecimal with a currency, or a long count of minor units, inside a Money type such as JSR 354's Moneta or an internal value class. Rounding uses the currency's scale and an explicit RoundingMode agreed with finance and tax law, applied only at defined steps, and the library designs out BigDecimal's pitfalls. Databases use NUMERIC columns, JSON carries amounts as strings, and the rollout uses a shared library, static analysis, reconciliation tests and a migration order based on financial exposure.
Why this question is asked
An invoice that is off by one cent sounds trivial until it is multiplied by millions of invoices, shows up in a tax audit, or makes the ledger fail to reconcile at month end. The cause is almost always the same: binary floating point used for decimal money, with rounding happening at different places in different services. Fixing one service is easy; making 15 teams agree on one standard and migrate safely is the senior part of the job, and that is what interviewers want to hear. In this answer, we will see why double causes the cent errors, define the money representation, set the rounding rules, list the BigDecimal pitfalls the library must hide, align the database and APIs, and plan the rollout across teams.
1Why double Produces Cent Errors
A double stores numbers as binary fractions, and most decimal amounts, such as 0.10 or 19.99, have no exact binary representation. Each operation rounds to the nearest representable value, and the tiny errors add up. Rounding then turns a value like 2.675, stored as 2.67499999999999982236431605997495353221893310546875, into 2.67 instead of the 2.68 that a person computing by hand expects.
Notice how inconsistent this is: the same stored amount becomes 2.68 through one method and 2.67 through another, because the result depends on hidden binary values rather than on the decimal amount anyone wrote down. With 15 services computing prices, taxes and conversions independently, some of them will disagree by a cent on some invoices, which is exactly what the audit found.
2Representing Money: BigDecimal With a Currency, or Minor Units
The first rule of the standard is the representation. An amount is always a number together with its currency, because 100 USD and 100 JPY are not the same thing and have different numbers of decimal places. The number is a BigDecimal, an exact decimal with an arbitrary scale (number of digits after the point), or a long count of minor units such as cents, which is fast and simple but needs care with currencies of three decimals and with exchange rates that need more precision.
Rather than passing BigDecimal and String currency codes around separately, we provide a value type. JSR 354, the Java Money API, defines MonetaryAmount, and Moneta is its reference implementation with Money backed by BigDecimal and FastMoney backed by a long. Many companies prefer a small internal record they fully control. Either way, arithmetic on mismatched currencies throws, and the only way to create an amount is through methods that enforce the scale.
Intermediate results, such as a unit price times a fractional quantity or a converted amount before rounding, may need more decimals than the currency. The library models those as a separate type or an explicit 'unrounded' amount, so they cannot be stored or invoiced by accident.
3Defining Rounding Rules Per Operation
Rounding is where services really diverge, so the standard names, for each operation, when rounding happens and which mode is used. Tax law often decides this: some jurisdictions require tax to be rounded per line item, others on the invoice total, and the mode may be half up. RoundingMode.HALF_EVEN, banker's rounding, rounds ties to the nearest even digit, which removes the upward bias of half up over many operations and is common in accounting and finance. Finance, tax and legal teams own these choices; engineering encodes them once.
The key principle is to round only at defined steps, typically when a value becomes a stored or displayed amount, not after every arithmetic operation, and never twice. A currency conversion, for example, multiplies by a rate with many decimals, then rounds once to the target currency's scale with the agreed mode.
| Operation | When to round | Mode (agreed with finance) |
|---|---|---|
| Line item price x quantity | per line, to currency scale | HALF_EVEN or as tax law requires |
| Tax | per line or per invoice, by jurisdiction | often HALF_UP by regulation |
| Currency conversion | once, after applying the rate | HALF_EVEN |
| Allocation or split | distribute the remainder cents | largest remainder, documented |
| Display only | never stored back | same as the stored value |
4BigDecimal Pitfalls the Library Must Hide
BigDecimal is exact, but it has three traps that cause bugs even in teams that use it. First, equals compares both value and scale, so 2.0 and 2.00 are not equal, and a HashSet of amounts can hold both. compareTo compares only the value. Second, the double constructor captures the binary error of the double exactly: new BigDecimal(0.1) is 0.1000000000000000055511151231257827021181583404541015625. We construct from a String or with BigDecimal.valueOf(double), which goes through the double's shortest decimal representation. Third, divide without a scale throws when the result has an infinite decimal expansion, such as 1 divided by 3, so every division must give a scale and RoundingMode.
The Money type hides all three: its scale is fixed by the currency, so equals and compareTo agree, it has no constructor from double, and its divide and allocate methods always take the rounding rules from the standard.
5Making the Database and APIs Match
Exact arithmetic in Java is pointless if the value is stored or transmitted as a binary float. In the database, money columns are NUMERIC or DECIMAL with an explicit precision and scale, for example NUMERIC(19, 4) for amounts that need sub-cent intermediate precision, plus a currency column, never FLOAT, DOUBLE PRECISION or REAL. With JPA, a BigDecimal field maps to NUMERIC with @Column(precision = 19, scale = 4).
In JSON, a number like 19.99 is parsed as a double by JavaScript and by many clients, which reintroduces the error. So the API standard serialises amounts as strings, for example {"amount": "19.99", "currency": "USD"}, and Jackson can be configured to read and write BigDecimal that way. The same applies to messages on Kafka and to CSV exports for finance.
6Rolling It Out Across 15 Services
A standard only helps if every team adopts it, so the rollout is a plan rather than an announcement. We publish the shared Money library with the rounding rules and documentation written together with finance. We add static analysis rules, for example custom Error Prone, ArchUnit or Semgrep rules, that flag double and float fields or parameters whose names suggest money (price, amount, total, tax), new BigDecimal(double), BigDecimal.equals and divide without a RoundingMode, starting as warnings and becoming build failures for new code.
Before changing any service, we build reconciliation tests: finance provides a set of invoices with expected results computed by their own process, including the cases that were off by a cent, and each service's output must match exactly. We then migrate in order of financial exposure: the services that produce invoices and tax first, then pricing, then reporting. Each migration runs in shadow mode for a while, computing old and new results side by side and logging differences, so we can explain every changed cent to finance before switching.
- Agree rounding rules with finance and taxwritten standard
- Ship the Money libraryvalue type, rounding, allocation
- Add static analysis ruleswarnings first, then build failures
- Build reconciliation test setsfinance's expected results
- Migrate by financial exposureinvoicing and tax first
- Shadow-compare old and newexplain every changed cent
- Switch, then remove double code paths
7 steps in order. Play them, or step through them one at a time.
7Follow-up questions
How do you split 100.00 into three parts so the total still adds up?
We cannot give each part 33.333..., and rounding each to 33.33 loses a cent, since three times 33.33 is 99.99. The standard allocation works in minor units: 10,000 cents divided by 3 is 3,333 with a remainder of 1, so every part gets 3,333 cents and the remainder cent goes to the first part, giving 33.34, 33.33 and 33.33, which sums to exactly 100.00. For weighted splits, the largest remainder method gives the leftover cents to the parts with the largest fractional remainders. The rule for who receives the extra cent must be documented, and the Money library provides an allocate method so every service does it the same way.
Should we choose BigDecimal or long cents?
long minor units are faster and avoid BigDecimal's pitfalls, and they work well for storing and summing amounts in currencies with fixed decimals. BigDecimal is better when we need intermediate precision, for exchange rates, interest or tax calculations, or currencies with three decimals. Many systems use BigDecimal in calculations and either NUMERIC or minor units at the storage boundary, as long as the conversion is done once in the shared library.
8Conclusion
In this answer, we set a standard for monetary arithmetic across 15 services. Money is a BigDecimal with a currency, or long minor units, inside a Money value type such as Moneta or an internal class, never a double. Rounding uses the currency's scale and an explicit mode agreed with finance and tax law, applied only at defined steps. The library hides BigDecimal's pitfalls with compareTo semantics, String or valueOf construction and divide with a scale and RoundingMode. Databases use NUMERIC columns and APIs send amounts as strings. The rollout uses a shared library, static analysis, reconciliation tests and a migration order based on financial exposure, with shadow comparisons before each switch.
Open with
The root cause is binary floating point: amounts like 0.10 cannot be stored exactly in a double, errors accumulate, and each service rounds at different points with different methods, so some invoices drift by a cent.
Then, if they want more
The standard I would set has four parts. First, representation: money is always an amount plus a currency, as a BigDecimal or a long of minor units, wrapped in a Money type, either Moneta from JSR 354 or a small internal class, and it can never be built from a double.
Second, rounding: the scale comes from the currency, two for dollars, zero for yen, and each operation has a documented rule, for example HALF_EVEN after a conversion and whatever the tax law demands for tax, applied once at defined steps. Finance owns those rules; we encode them.
Third, the library hides BigDecimal's traps: compare with compareTo because 2.0 and 2.00 are not equal, construct from strings or valueOf, and always give a scale and RoundingMode to divide. Fourth, storage and transport: NUMERIC columns in the database and amounts as strings in JSON so clients do not parse them into doubles.
For the rollout, I would ship the shared library, add static analysis that flags doubles in money code, build reconciliation tests from finance's expected invoices, migrate the services with the most financial exposure first, and run old and new side by side until every changed cent is explained.
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 official documentation for the version you use. 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 "An audit finds that 15 services calculate prices, taxes and currency conversions with...": https://scrivio-interview-library.netlify.app/java/coding-problems/e9c3a4d437/