Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Two services exchange monetary amounts. One serializes BigDecimal as a JSON number, the other parses JSON numbers as double by default, and amounts drift. How do you fix it across the system and prevent it in new services?

JSON numbers have no defined precision, and many parsers, including JavaScript and untyped Java bindings, read them as double by default, so exact decimal amounts lose digits on the way in. The lasting fix is to change the contract so amounts travel as decimal strings or as integer minor units with an explicit currency field; as a stopgap we configure parsers to read decimals exactly, for example Jackson's USE_BIG_DECIMAL_FOR_FLOATS. We version the API and migrate consumers, running old and new fields side by side during the transition, and we record the rule in shared API guidelines backed by contract tests.

Why this question is asked

Bugs at the boundary between services are among the hardest to own, because each service is correct by its own standard: one sends an exact BigDecimal, the other reads a perfectly valid JSON number. The drift appears only when the two are put together, often weeks later in reconciliation. Interviewers ask this scenario at senior levels because the fix is not one line of code; it is a contract change across teams. In this answer, we will recap what JSON says about numbers, reproduce the drift, compare the ways to represent amounts, apply a stopgap, migrate the contract without breaking consumers, and make sure new services get it right from the start.

1A Quick Recap: What JSON Says About Numbers

The JSON format defines the syntax of a number, digits with an optional fraction and exponent, but not its precision or how it must be stored. RFC 8259 notes that implementations commonly use IEEE 754 double precision, and that numbers beyond that precision may not be handled consistently. So a JSON number such as 19.99 is just text; what it becomes depends entirely on the parser. JavaScript's JSON.parse always produces a double. Java libraries such as Jackson produce whatever type the target field declares, but fall back to Double when the target is untyped, such as Object or Map<String, Object>.

2Reproducing the Drift

The producer writes a BigDecimal as a JSON number, which is exact on the wire. The consumer binds it to a double field, or reads it into a generic map, and the value becomes the nearest binary floating point number. Single values often print correctly, which hides the problem, but sums and large values do not.

Java
// Producer: exact
record Payment(BigDecimal amount, String currency) {}
mapper.writeValueAsString(new Payment(new BigDecimal("12345678901234567.89"), "EUR"));
// {"amount":12345678901234567.89,"currency":"EUR"}
// Consumer: double field
record PaymentView(double amount, String currency) {}
PaymentView v = mapper.readValue(json, PaymentView.class);
System.out.println(v.amount()); // 1.2345678901234568E16: the cents are gone
// Consumer: untyped map
Map<String, Object> m = mapper.readValue("{\"amount\":0.1}", Map.class);
System.out.println(m.get("amount").getClass()); // class java.lang.Double
double sum = 0.1 + 0.2; // 0.30000000000000004 after adding two amounts
Step throughHow an exact amount drifts between services
Billing serviceJSON on the wireLedger service
write BigDecimal 0.10 as number 0.10
parse 0.10 into a double field
add 0.10 + 0.20 = 0.30000000000000004
reconciliation reports a mismatch

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

Each side is correct on its own; the contract leaves the precision undefined.

Two effects combine here. Large amounts lose their low digits because a double holds only about 15 to 17 significant digits. Ordinary amounts look correct one at a time but drift when the consumer adds or multiplies them, because 0.1 and 0.2 have no exact binary form.

3Choosing How Amounts Travel

There are two robust representations. The first is a decimal string: {"amount": "19.99", "currency": "EUR"}. Every parser in every language keeps a string exactly, and the consumer converts it with new BigDecimal(text). The second is integer minor units: {"amountMinor": 1999, "currency": "EUR"}. Integers are exact in double up to 2^53, about 90 trillion in currency units of cents, so even JavaScript handles them exactly in practice, and the currency tells the consumer how many decimals to apply.

Both carry an explicit currency, because an amount without a currency is ambiguous and its scale is unknown. Strings are more self-describing and handle currencies with three decimals naturally. Minor units are compact and convenient for arithmetic, but every consumer must know the currency's fraction digits. Either is fine; what matters is choosing one for the platform and stating it in the contract.

4A Stopgap While the Contract Changes

While we migrate, we can stop the loss in Java consumers that we control. Jackson reads JSON numbers exactly when the target field is BigDecimal, so the first step is to change double fields for money to BigDecimal. For untyped bindings, the feature DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS makes Jackson produce BigDecimal instead of Double for floating numbers. On the producer side, @JsonFormat(shape = JsonFormat.Shape.STRING) on a BigDecimal field serialises it as a string.

Java
ObjectMapper mapper = JsonMapper.builder()
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
.build();
Map<String, Object> m = mapper.readValue("{\"amount\":0.1}", Map.class);
System.out.println(m.get("amount").getClass()); // class java.math.BigDecimal
// In Spring Boot (Jackson 2): spring.jackson.deserialization.use-big-decimal-for-floats=true

This is a stopgap, not a fix: it does nothing for consumers in JavaScript, other languages, or libraries we do not control. Spring Boot 4 moves to Jackson 3, where package and builder names changed, so check your version for the exact feature and property names.

5Migrating the Contract Without Breaking Consumers

The real fix is a versioned contract change. We add the new representation next to the old one, for example keeping "amount" as a number and adding "amountValue": "19.99" (or a new API version with the string field), and mark the old field as deprecated in the schema. Producers write both. Consumers move to the new field one at a time, verified by comparing the two values in logs or metrics during the transition. When usage monitoring or the consumer list shows that nobody reads the old field, we remove it in the next major version.

During the transition, a reconciliation report that compares totals computed from both fields tells us whether any consumer still relies on the lossy one, and gives a clear signal when the migration is complete.

6Preventing It in New Services

Finally, we make the rule part of how APIs are designed. The shared API guidelines state that monetary amounts are strings (or minor units, whichever the platform chose) with a currency field, never JSON numbers with fractions. A shared schema component for Money in the OpenAPI specifications makes the right shape the default, and a schema linter rejects monetary properties typed as number. Consumer-driven contract tests, with tools such as Spring Cloud Contract or Pact, include amounts with many digits and tie cases, so a consumer that parses them as double fails in CI rather than in reconciliation.

7Follow-up questions

Why not keep JSON numbers and just require BigDecimal parsing everywhere?

Because we cannot control every consumer. JavaScript, many dynamic languages and generic tools such as logging pipelines read numbers as doubles, and even in Java a single untyped mapping is enough to lose precision. A string or an integer is exact for every parser, so the contract does not depend on each consumer configuring things correctly.

Is an integer in minor units safe for JavaScript consumers?

Yes for realistic amounts, because doubles represent every integer exactly up to 2^53, which is about 9 x 10^15 minor units. Amounts beyond that are unlikely in payments, but for very large values, or for currencies with many decimals, strings remain the safest choice.

How do you find every consumer of the old field?

From the API gateway or service mesh logs, which show who calls the endpoint, from the contract test registry, which lists consumers and the fields they use, and by asking teams to confirm. For events on a message broker, the consumer groups on the topic give the list.

8Conclusion

In this answer, we saw that JSON leaves number precision undefined and many parsers read numbers as double, so a BigDecimal sent as a number loses digits in a consumer that binds it to double or to an untyped map. The lasting fix is a contract that sends amounts as decimal strings or integer minor units with a currency. As a stopgap, Java consumers read BigDecimal fields or enable USE_BIG_DECIMAL_FOR_FLOATS. We migrated with versioned, side-by-side fields and prevented recurrence with API guidelines, a shared Money schema, schema linting and contract tests.

What to say in the interviewAbout 100 seconds aloud

Open with

The root cause is that JSON does not define number precision. The producer writes an exact BigDecimal as a JSON number, but the consumer's parser decides what it becomes, and many default to double: JavaScript always does, and in Java any double field or untyped Map binding does.

Then, if they want more

So large amounts lose their low digits and ordinary amounts drift when they are added. I would fix it at three levels. Immediately, as a stopgap, change money fields in the Java consumers to BigDecimal and, where there is untyped binding, enable Jackson's USE_BIG_DECIMAL_FOR_FLOATS, checking the exact names on Jackson 3 if we are on Spring Boot 4.

That stops the loss but only for consumers we control. The real fix is the contract: amounts travel as decimal strings, or as integer minor units, always with an explicit currency field, because every parser keeps strings and integers exact.

I would introduce that as a versioned change: add the new field next to the old one, producers write both, consumers migrate one by one, we compare the two values during the transition, and we remove the old field in the next major version once monitoring shows nobody reads it.

To prevent it in new services, I would put the rule into the shared API guidelines, provide a shared Money schema component, lint OpenAPI specs for monetary fields typed as number, and add contract tests with long, many-digit amounts so a consumer that parses as double fails in CI.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4
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 "Two services exchange monetary amounts. One serializes BigDecimal as a JSON number, the...": https://scrivio-interview-library.netlify.app/java/types-and-values/7bc4e6c8c6/