I am writing a program to help students practice division, how can I compare the user Input and the correct answer if they are doubles
Asked
Active
Viewed 67 times
2 Answers
0
You can convert it to BigDecimal, and compare with the required precision of decimal places
BigDecimal aa = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
aa = aa.setScale(4, BigDecimal.ROUND_DOWN);
bb = bb.setScale(4, BigDecimal.ROUND_DOWN);
return aa.equals(bb);

Shevtsov
- 140
- 9
0
Either use
Double.compare()
or
final double EPSILON = 0.00000001d;
Math.abs(firstDouble - secondDouble) < EPSILON;
it's based on level of accuracy you needed for comparison

Sam
- 4,046
- 8
- 31
- 47