BigDecimal and equals()

S

Sideswipe

I am having an issue with BigDecimal and compareTo() not being
consistent with equals. I have read the documentation and know why
it's behaving that way, but I don't understand why they would
intentionally make compareTo() inconsistent with equals()

More importantly though, When I perform an operation (division in this
case) I get results with different scale even though the MathContext
is identical for all steps. Code below. EXPECTED has a scale of 15 and
result has a scale of 16.

BigDecimal A = new BigDecimal(3.21, MathContext.DECIMAL64);
BigDecimal TEN = new BigDecimal(10, MathContext.DECIMAL64);
BigDecimal EXPECTED = new BigDecimal(.321,MathContext.DECIMAL64);

BigDecimal result = A.divide(TEN,MathContext.DECIMAL64);

System.out.println("equals? " + result.equals(EXPECTED));
System.out.println("compareTo() ? " + result.compareTo(EXPECTED));

equals? false
compareTo() ? 0

How can I control this process so that compareTo() is consistent with
equals()?

Christian Bongiorno
http://christian.bongiorno.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top