0

The following code:

 BigDecimal a = new BigDecimal("8900");
 BigDecimal b = new BigDecimal("8.9E+3");

 System.out.println(a.equals(b));

prints false. Why is so, if mathematically those numbers are equal?

internal representation

Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129

1 Answers1

1

You have to use compareTo():

BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");

System.out.println(a.compareTo(b) == 0);
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71