Scrivio Interview library
CodingIntermediate7 min read · updated 12 November 2026

Design a Money value object in Java for a payments service. Which representation, operations and checks do you include, and which mistakes do you avoid?

A good Money value object stores the amount as a BigDecimal and the currency as java.util.Currency, never as a double, and normalises the amount to the currency's default fraction digits with an explicit rounding mode. It is immutable, typically a record that validates in its compact constructor, and its operations such as plus and minus check that both currencies match. Because BigDecimal.equals compares scale (2.0 is not equal to 2.00), we normalise the scale or compare with compareTo, and we keep currency conversion out of Money, since it needs exchange rates and belongs in a service.

Why this question is asked

Every payments system has a Money type, and many of the bugs in payments systems come from getting it slightly wrong: a cent lost to floating point, a refund in the wrong currency, a duplicate detected as different because one amount had an extra zero. Interviewers ask this question because it tests both domain modelling and careful knowledge of BigDecimal. In this answer, we will see why double fails, choose the representation, handle scale and rounding, build the record with its operations, look at the equality trap, and decide where conversion belongs.

1Why double Is the Wrong Type

A double stores numbers in binary floating point. Most decimal fractions, such as 0.1, cannot be represented exactly in binary, so the value stored is a close approximation. For scientific work that is fine; for money it is not, because the small errors appear in totals and comparisons.

Java
double a = 0.1 + 0.2;
System.out.println(a); // 0.30000000000000004
System.out.println(a == 0.3); // false
BigDecimal b = new BigDecimal("0.1").add(new BigDecimal("0.2"));
System.out.println(b); // 0.3
System.out.println(new BigDecimal(0.1)); // 0.1000000000000000055511151231257827... (built from the double)

BigDecimal stores an exact decimal number as an unscaled integer and a scale, the number of digits after the decimal point. Notice the last line: we must build it from a String or from BigDecimal.valueOf, never with new BigDecimal(double), or we copy the double's error into it.

2Choosing the Representation and the Scale

Our Money holds two things: a BigDecimal amount and a java.util.Currency. Currency is the JDK's representation of ISO 4217 currencies, and it knows how many minor units each currency uses through getDefaultFractionDigits(): 2 for EUR and USD, 0 for JPY, 3 for KWD.

We normalise every amount to that scale with an explicit rounding mode. The reason is that the same value can otherwise appear as 10, 10.0 and 10.00, which print differently and, as we will see, compare differently. Rounding must be explicit because setScale without a mode throws ArithmeticException when digits would be lost, and because the business decides how to round. HALF_EVEN, often called banker's rounding, avoids a systematic upward bias across many operations, but some domains require HALF_UP, so it should be a deliberate choice.

Default fraction digits by currency
CurrencygetDefaultFractionDigits()12.345 normalised (HALF_EVEN)
EUR212.34
USD212.34
JPY012
KWD312.345
12.345 to two digits with HALF_EVEN gives 12.34, because 4 is even; HALF_UP would give 12.35.

3Building the Record

A record gives us immutability and value-based equality for free. The compact constructor runs before the fields are assigned, so it is the right place to validate and normalise.

Java
public record Money(BigDecimal amount, Currency currency) {
private static final RoundingMode ROUNDING = RoundingMode.HALF_EVEN;
public Money {
Objects.requireNonNull(amount, "amount");
Objects.requireNonNull(currency, "currency");
amount = amount.setScale(currency.getDefaultFractionDigits(), ROUNDING);
}
public static Money of(String amount, String code) {
return new Money(new BigDecimal(amount), Currency.getInstance(code));
}
public Money plus(Money other) { requireSame(other); return new Money(amount.add(other.amount), currency); }
public Money minus(Money other) { requireSame(other); return new Money(amount.subtract(other.amount), currency); }
public Money times(BigDecimal factor) { return new Money(amount.multiply(factor), currency); }
public boolean isNegative() { return amount.signum() < 0; }
private void requireSame(Money other) {
if (!currency.equals(other.currency))
throw new IllegalArgumentException("currency mismatch: " + currency + " vs " + other.currency);
}
}
Money a = Money.of("10", "EUR");
System.out.println(a.amount()); // 10.00
System.out.println(a.plus(Money.of("2.5", "EUR"))); // Money[amount=12.50, currency=EUR]
a.plus(Money.of("1", "USD")); // IllegalArgumentException: currency mismatch: EUR vs USD
System.out.println(Money.of("19.99", "EUR").times(new BigDecimal("0.15")).amount()); // 3.00

Notice that every operation returns a new Money, and the constructor normalises the result, so times() rounds 2.9985 to 3.00 with the same rule everywhere. Adding EUR to USD fails loudly instead of producing a meaningless number. Whether negative amounts are allowed depends on the domain; a ledger needs them for debits, so here we only offer isNegative() and let callers decide.

4The BigDecimal Equality Trap

BigDecimal.equals compares both the value and the scale. So 2.0 and 2.00 are not equal, and they also have different hash codes. compareTo compares only the numeric value.

Java
BigDecimal x = new BigDecimal("2.0");
BigDecimal y = new BigDecimal("2.00");
System.out.println(x.equals(y)); // false: scale 1 vs scale 2
System.out.println(x.compareTo(y)); // 0: numerically equal
System.out.println(Money.of("2.0", "EUR").equals(Money.of("2.00", "EUR"))); // true: both normalised to 2.00

This is the main reason we normalise the scale in the constructor. Because every Money of the same currency has the same scale, the record's generated equals and hashCode, which call BigDecimal.equals, behave as the business expects. Without normalisation, a duplicate-payment check using a HashSet of Money would treat 2.0 EUR and 2.00 EUR as different payments.

5Keeping Conversion Out of Money

It is tempting to add a convertTo(Currency) method. But conversion needs an exchange rate, the rate depends on time and on a provider, and the business has rules about which rate to use for which transaction. None of that belongs inside a value object, which should be pure and need no outside data.

So conversion goes into a domain service, such as a CurrencyConverter, that takes a Money, a target currency and a rate or a rate source, and returns a new Money. That keeps Money simple to test, and it makes every conversion visible in the code that performs it. Allocation, splitting an amount into parts without losing a cent, is also often a separate method or service, because naive division can leave a remainder that must be assigned to one part.

6Follow-up questions

Why not store money as a long number of minor units?

That is a valid choice and is common in high-volume systems: 1999 cents for 19.99 EUR. It is fast and exact. The cost is that we must always know the currency's fraction digits to display or convert it, and operations like percentages still need careful rounding. Inside a Money type the internal form can be either, as long as the API stays the same.

What about the JSR 354 Money API?

JSR 354, with the reference implementation Moneta, provides MonetaryAmount types, rounding and conversion providers. It is a reasonable choice for complex needs, but many teams prefer a small Money record they fully control, because it is easy to read and has no extra dependency.

How do you store Money in a database?

Usually as two columns: a DECIMAL or NUMERIC amount with enough precision, such as NUMERIC(19, 4), and a three-letter currency code. In JPA, Money can be an @Embeddable, or a pair of columns mapped with a converter for the Currency.

7Conclusion

In this answer, we built a Money value object for a payments service. It stores the amount as a BigDecimal and the currency as java.util.Currency instead of a double, normalises the scale to the currency's default fraction digits with an explicit rounding mode, and is an immutable record that validates in its compact constructor. Its plus and minus methods check that the currencies match. We saw that BigDecimal.equals considers scale, so we normalise or use compareTo, and that currency conversion belongs in a service because it needs rates.

What to say in the interviewAbout 90 seconds aloud

Open with

I would make Money a record with two components: a BigDecimal amount and a java.util.Currency. Never a double, because binary floating point cannot represent most decimal fractions exactly, so 0.1 plus 0.2 is not 0.3, and that turns into lost cents.

Then, if they want more

I also never build BigDecimal from a double, only from a string or valueOf. In the compact constructor I check for nulls and normalise the amount with setScale to the currency's default fraction digits, two for euros, zero for yen, three for Kuwaiti dinar, with an explicit rounding mode such as HALF_EVEN, chosen with the business.

Normalising matters because BigDecimal.equals compares scale, so 2.0 and 2.00 are not equal; once every Money in a currency has the same scale, the record's equals and hashCode work as expected. Otherwise I would have to compare with compareTo.

The operations, plus, minus and times, all return new instances, and plus and minus throw if the currencies differ, so we can never add euros to dollars by accident. Whether negatives are allowed depends on the domain. Finally, I keep conversion out of Money.

Converting needs an exchange rate, a provider and rules about which rate applies when, so it belongs in a domain service that takes a Money and returns a new one. That keeps Money pure and easy to test.

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 "Design a Money value object in Java for a payments service. Which representation...": https://scrivio-interview-library.netlify.app/java/domain-driven-design/df2d4d275c/