Quick revision
The short answer
I would make Money an immutable record or final class holding a BigDecimal amount and a java.util.Currency, validated in the constructor so an invalid amount can never exist. Because BigDecimal.equals treats 2.0 and 2.00 as different, the constructor normalises the scale to the currency's fraction digits, or equality is defined with compareTo. Arithmetic returns new instances, refuses to mix currencies, and every division or conversion names its rounding mode explicitly. Being immutable, Money values can be cached, shared between threads and used as map keys safely.
What to remember
In this answer, we designed Money as an immutable record of a BigDecimal amount and a Currency, validated in its constructor. We saw that BigDecimal.equals includes the scale, so we normalise every amount to the currency's fraction digits with RoundingMode.UNNECESSARY, which also makes the generated equals and hashCode correct. Arithmetic returns new instances and refuses to mix currencies, rounding is always explicit, and splitting distributes remainders so totals stay exact. Immutability then lets us cache, share and use Money as a map key safely.
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.
I would make Money an immutable record or final class holding a BigDecimal amount and a java.util.Currency, validated in the constructor so an invalid amount can never exist. Because BigDecimal.equals treats 2.0 and 2.00 as different, the constructor normalises the scale to the currency's fraction digits, or equality is defined with compareTo. Arithmetic returns new instances, refuses to mix currencies, and every division or conversion names its rounding mode explicitly. Being immutable, Money values can be cached, shared between threads and used as map keys safely.
Why this question is asked
Money looks simple until a billing system loses a cent on every thousandth invoice, or two services disagree about whether 10.0 EUR equals 10.00 EUR. A shared value type removes that whole class of bugs, but only if its design decisions are made deliberately once, rather than differently in every service. Interviewers ask this question to see whether we know the traps of BigDecimal and how immutability makes a type safe to share. In this answer, we will look at why double is ruled out, choose the shape of the type, decide equality and scale, define arithmetic and rounding, and see why immutability pays off.
1A Quick Recap: Why Not double?
A double stores numbers in binary, and most decimal fractions such as 0.1 have no exact binary form. So arithmetic on doubles produces tiny errors that show up as wrong cents once they add up. BigDecimal stores a decimal number exactly, as an unscaled integer and a scale, the number of digits after the decimal point: 12.50 is the integer 1250 with scale 2.
Notice the last line: creating a BigDecimal from a double copies the binary error. We always create amounts from strings, from long minor units such as cents, or with BigDecimal.valueOf.
2The Shape of the Type
Money is a value: two Money objects with the same amount and currency are interchangeable, and nobody cares which instance they hold. Values should be immutable, and a record expresses that with the least code. Its components are a BigDecimal amount, which is itself immutable, and a java.util.Currency, which is a shared instance per ISO 4217 code such as EUR or JPY and also immutable. Both components are immutable, so the record is deeply immutable without any copying.
The constructor is the single gate through which every Money passes, so it does all validation: no null amount or currency, and an amount whose scale fits the currency. If invalid values cannot be constructed, no other code has to check for them.
RoundingMode.UNNECESSARY means: change the scale, but throw ArithmeticException if that would lose a digit. So 10.5 EUR becomes 10.50, while 10.505 EUR is rejected instead of silently rounded. Rounding is a business decision and belongs in the operation that causes it, not in the constructor.
3Deciding Equality: 2.0 Versus 2.00
BigDecimal.equals compares both the value and the scale, so new BigDecimal("2.0").equals(new BigDecimal("2.00")) is false, and their hash codes differ too. compareTo compares only the numeric value and returns 0 for them. A record's generated equals uses the component's equals, so without care, Money.of("2.0", "EUR") and Money.of("2.00", "EUR") would be different keys in a HashMap.
| Expression | Result | Why |
|---|---|---|
| 2.0 equals 2.00 | false | same value, different scale |
2.0 compareTo 2.00 | 0 | compares the value only |
2.0 hashCode vs 2.00 hashCode | different | the hash includes the scale |
| Money 2.0 EUR equals Money 2.00 EUR | true | constructor normalises both to scale 2 |
We have two ways to make Money behave. The one I prefer is the constructor above: every amount is normalised to the currency's scale, so equal values always have equal scales and the generated equals and hashCode are correct. The alternative is to write equals with compareTo and hashCode with stripTrailingZeros(), which works but is easy to get wrong. Normalising once is simpler and also makes toString and serialisation consistent: every EUR amount prints with two decimals.
4Arithmetic That Returns New Values and Refuses Mixing
Every operation returns a new Money and leaves both operands unchanged. Adding EUR to USD is never meaningful without an exchange rate, so the method refuses it rather than producing a number with the wrong currency.
5Rounding and Splitting Explicitly
Multiplying by a tax rate or dividing a bill produces more digits than the currency has, so someone must decide how to round. BigDecimal.divide without a rounding mode throws ArithmeticException for results like 10 / 3, which is good: it forces the decision. Our API takes a RoundingMode on every operation that may need one, and the team picks the mode per business rule, such as HALF_UP for customer-facing prices or HALF_EVEN, often called banker's rounding, for accounting totals.
Splitting deserves its own method, because rounding each part loses money: 10.00 / 3 rounded gives 3.33 three times, which is 9.99. An allocate method gives the leftover cent to the first parts so the total is exact.
We work in minor units: 1000 cents divided by 3 is 333 with a remainder of 1, so the parts are 3.34, 3.33 and 3.33, which add up to 10.00 exactly. Conversion between currencies follows the same principle: it is a separate operation that takes a rate and a rounding mode and returns Money in the target currency, never a silent change of the currency field.
6Why Immutability Pays Off Here
Because Money cannot change, we can share one instance everywhere. A constant such as Money.zero(EUR) can be cached and reused, prices can sit in a cache shared by all request threads without locks, and Money can be a HashMap key, for example to group totals by amount, because its hashCode never changes. A mutable Money would need defensive copies every time it crossed a method boundary, and a single accidental amount.add-in-place in one service would corrupt every holder of that object.
The remaining decisions are about edges: how Money is serialised in JSON (amount as a string, never a number, so clients do not parse it as a double), and how it maps to the database (a NUMERIC column with the right scale plus a currency code column, or minor units in a BIGINT).
7Follow-up questions
Why not store the amount as a long number of cents?
It is a valid choice and often faster: integer arithmetic is exact and cheap. The costs are that the scale differs per currency (JPY has 0 digits, BHD has 3), so code must always know the currency to interpret the number, and fractional results such as tax rates still need careful rounding. Many systems use minor units in storage and BigDecimal in the domain type.
Should Money use the record's generated toString?
It is fine for logs, giving Money[amount=12.50, currency=EUR]. For display to users we use a formatter with a Locale, such as NumberFormat.getCurrencyInstance, because the symbol and separators differ by country. Keeping formatting out of the value type keeps it simple.
How would you compare amounts for ordering?
Money could implement Comparable<Money> with compareTo on the amounts, after checking that the currencies match. Comparing amounts in different currencies has no meaning without a rate, so it should throw rather than guess.
8Conclusion
In this answer, we designed Money as an immutable record of a BigDecimal amount and a Currency, validated in its constructor. We saw that BigDecimal.equals includes the scale, so we normalise every amount to the currency's fraction digits with RoundingMode.UNNECESSARY, which also makes the generated equals and hashCode correct. Arithmetic returns new instances and refuses to mix currencies, rounding is always explicit, and splitting distributes remainders so totals stay exact. Immutability then lets us cache, share and use Money as a map key safely.
Open with
I would make Money an immutable record of a BigDecimal amount and a java.util.Currency. BigDecimal because double cannot represent most decimal fractions, and I would never build it from a double.
Then, if they want more
Both components are immutable, so the record is deeply immutable, and the compact constructor is the one place to validate: no nulls, and the amount must fit the currency's fraction digits. Equality is the main trap, because BigDecimal.equals compares scale, so 2.0 and 2.00 are different and have different hash codes.
I would normalise in the constructor with setScale to the currency's default fraction digits and RoundingMode.UNNECESSARY, which pads 10.5 to 10.50 but throws for 10.505 instead of rounding silently, and then the generated equals and hashCode are correct.
All arithmetic returns new Money, and plus or minus on different currencies throws, because mixing them needs an explicit conversion with a rate.
Any operation that can produce extra digits, like applying a tax rate, dividing or converting, takes a RoundingMode argument so the business rule is visible, and splitting a bill distributes the leftover cents so the parts add up exactly. Because Money never changes, we can cache constants, share values across threads without locks and use them as map keys.
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 "You are designing a Money value type used across billing services. What decisions do you...": https://scrivio-interview-library.netlify.app/java/immutability/9019f666c1/