How to compare doubles in Java?

To compare doubles in Java, do this: Math.abs(double1 - double2) < 0.000001d.

Here's how you do it:

// both should be 0.6, but one of them isn't… 🤯
double double1 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
double double2 = 0.1 * 6;

// so, don't do this ✋
boolean incorrectCompare = double1 == double2;

// do this instead 👇
boolean correctCompare = Math.abs(double1 - double2) < 0.000001d;

// see? 🤓
System.out.println(incorrectCompare); // false
System.out.println(correctCompare); // true