Quick revision
The short answer
new BigDecimal(0.1) captures the exact binary value of the double, 0.1000000000000000055511151231257827021181583404541015625, so we should use new BigDecimal("0.1") or BigDecimal.valueOf(0.1). equals compares both the value and the scale, so 2.50 and 2.5 are not equal; compareTo is the numeric comparison. And dividing 1 by 3 without a scale or MathContext throws ArithmeticException because the exact result never terminates, so we pass a scale and a RoundingMode, for example divide(x, 2, RoundingMode.HALF_EVEN).
What to remember
In this answer, we saw three BigDecimal traps. The double constructor captures the binary error of the literal, so we create values from strings or with BigDecimal.valueOf. equals compares value and scale, so 2.50 and 2.5 differ and we use compareTo for numeric equality. divide without a scale or MathContext throws for non-terminating results such as 1 / 3, so we pass a scale and a RoundingMode chosen from the business rule. We also saw why HashSet and TreeSet disagree about 2.5 and 2.50.
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.
new BigDecimal(0.1) captures the exact binary value of the double, 0.1000000000000000055511151231257827021181583404541015625, so we should use new BigDecimal("0.1") or BigDecimal.valueOf(0.1). equals compares both the value and the scale, so 2.50 and 2.5 are not equal; compareTo is the numeric comparison. And dividing 1 by 3 without a scale or MathContext throws ArithmeticException because the exact result never terminates, so we pass a scale and a RoundingMode, for example divide(x, 2, RoundingMode.HALF_EVEN).
Background to this question
BigDecimal is the type we reach for when numbers must be exact, mostly for money. Ironically, it has a few traps that produce inexact or surprising results if we use it the way we use double. Each line in this question is one of those traps, and each one has caused real production bugs: wrong amounts, failed equality checks and exceptions on the first non-terminating division. In this answer, we will recap how BigDecimal stores a number, then go through the three lines one by one, showing the failure and the fix, and finish with why collections treat 2.5 and 2.50 differently.
1A Quick Recap: Unscaled Value and Scale
A BigDecimal stores two things: an unscaled value, which is an arbitrary-size integer, and a scale, which says how many digits are to the right of the decimal point. The number is the unscaled value divided by ten to the power of the scale. So 2.50 is the unscaled value 250 with scale 2, and 2.5 is 25 with scale 1. They are the same number, but not the same representation.
Because it is decimal, BigDecimal can represent 0.1 exactly, unlike double. It is also immutable: every operation returns a new BigDecimal, so we must use the returned value.
2Line 1: new BigDecimal(0.1)
The argument 0.1 is a double literal, so by the time the constructor sees it, the value has already been rounded to the nearest binary fraction. The double constructor is exact: it faithfully converts that binary value, error included.
The String constructor is the most direct fix, because the decimal digits never pass through binary. BigDecimal.valueOf(double) is fine when we only have a double, because it goes through Double.toString, which gives the shortest decimal that identifies the double. Static analysis tools commonly flag the double constructor for this reason.
3Line 2: equals Compares the Scale Too
BigDecimal.equals returns true only when both the unscaled value and the scale are equal. That is a deliberate design choice: 2.50 and 2.5 may mean different things, such as a price known to the cent versus a rough value. For numeric equality, we use compareTo, which ignores the scale.
| Expression | Unscaled value | Scale | equals 2.5? | compareTo 2.5 |
|---|---|---|---|---|
| 2.5 | 25 | 1 | true | 0 |
| 2.50 | 250 | 2 | false | 0 |
| 2.500 | 2500 | 3 | false | 0 |
This trap often appears in tests that compare an amount read from the database, which has the column's scale such as 2.50, with an expected value written as new BigDecimal("2.5"). AssertJ's isEqualByComparingTo, or comparing with compareTo, avoids it. Normalising with setScale to the currency's scale before storing or comparing is another option.
4Line 3: divide Without a Scale
The one-argument divide tries to compute the exact quotient. For 1 / 3 the exact decimal is 0.333... forever, which BigDecimal cannot hold, so it throws instead of guessing how to round.
Notice that the method works for divisors such as 4 and fails only for some inputs, which is why the bug often slips through tests and appears in production when a real value produces a repeating decimal. The safe habit is to always pass a scale and a RoundingMode, or a MathContext that sets the total precision, and to choose them from the business rule: two decimals with HALF_EVEN for many currency calculations, or higher precision for intermediate rates.
5Follow-up questions
Why can a HashSet<BigDecimal> contain both 2.5 and 2.50, while a TreeSet<BigDecimal> contains one?
HashSet uses equals and hashCode, and both include the scale, so 2.5 and 2.50 are different elements. TreeSet uses compareTo, which only looks at the numeric value, so it sees 2.50 as a duplicate of 2.5 and keeps one. The BigDecimal documentation points out that its natural ordering is inconsistent with equals for exactly this reason. Normalising values with setScale or stripTrailingZeros before adding them makes both sets agree.
Which RoundingMode should we use for money?
It depends on the business and sometimes on regulation. HALF_EVEN, also called banker's rounding, rounds ties to the even neighbour, so 0.125 becomes 0.12 and 0.135 becomes 0.14, which avoids a systematic upward bias over many operations. HALF_UP, where 0.125 becomes 0.13, is what most people learned at school and is required by some tax rules. The important part is to choose deliberately and use the same rule everywhere.
Is BigDecimal.valueOf(0.1) always safe?
It gives the decimal that Double.toString produces, which is what people usually mean. But if the double itself came from an inexact calculation, such as 0.1 + 0.2, valueOf faithfully gives 0.30000000000000004. The real fix is to keep values in BigDecimal from the start, ideally parsed from strings.
6Conclusion
In this answer, we saw three BigDecimal traps. The double constructor captures the binary error of the literal, so we create values from strings or with BigDecimal.valueOf. equals compares value and scale, so 2.50 and 2.5 differ and we use compareTo for numeric equality. divide without a scale or MathContext throws for non-terminating results such as 1 / 3, so we pass a scale and a RoundingMode chosen from the business rule. We also saw why HashSet and TreeSet disagree about 2.5 and 2.50.
Open with
Each line has a different trap. The first one, new BigDecimal(0.1), passes a double, and 0.1 cannot be represented exactly in binary, so the constructor faithfully captures the binary value, 0.1000000000000000055511151231257827 and so on.
Then, if they want more
The fix is the String constructor, new BigDecimal("0.1"), or BigDecimal.valueOf(0.1), which goes through Double.toString and gives 0.1. The second, new BigDecimal("2.50").equals(new BigDecimal("2.5")), is false, because a BigDecimal is an unscaled integer plus a scale, and equals compares both: 250 with scale 2 versus 25 with scale 1.
For numeric equality I use compareTo, which returns zero here, or normalise the scale first. That also explains the follow-up: a HashSet uses equals and hashCode, so it keeps both, while a TreeSet uses compareTo and keeps one. The third, BigDecimal.ONE.divide(BigDecimal.valueOf(3)), throws ArithmeticException, non-terminating decimal expansion, because the one-argument divide must return the exact result and one third never terminates.
It works for divisors like 4, which is why it slips through tests. The fix is to always specify how to round: divide with a scale and a RoundingMode, for example 2 and HALF_EVEN for currency, or a MathContext for precision. I choose those from the business rule and apply them consistently.
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 "What is wrong with each line?": https://scrivio-interview-library.netlify.app/java/types-and-values/fd69a2459c/