Louise Lin
Louise Lin

Reputation: 335

how to compare two doubles to the nearest hundredth digit in java

I tried to multiply each of the double by 100 and then use Math. around() before doing Math.abs() < 0.00000001 but it doesn't work.

double A = Math.sqrt(3);  
double B = Math.sqrt(5);  
Boolean result = Math.abs(Math.round(A * 100.0)/100.0 - Math.round(B * 100.0)/100.0) < 0.00000001

Upvotes: 0

Views: 142

Answers (1)

Thomas Kl&#228;ger
Thomas Kl&#228;ger

Reputation: 21455

Math.sqrt(3) rounded to two digits is 1.73, Math.sqrt(5) rounded to two digits is 2.24. How do you expect these two values to match?

Upvotes: 1

Related Questions