Basically you shouldn't do exact comparisons, you should do something like this:

double a = 1.000001;
double b = 0.000001;
double c = a-b;
if (Math.abs(c-1.0) <= 0.000001) {...}
Answer from Kevin on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java numbers › comparing doubles in java
Comparing Doubles in Java | Baeldung
December 11, 2025 - The recommended algorithm to compare double values in plain Java is a threshold comparison method.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › double_compare.htm
Java - Double compare() method
We're having two double values and using compare() method, we're comparing these double values and then result is compared to 0. If result is greater than 0 then first number is greater than second number.
🌐
DEV Community
dev.to › pavel_polivka › double-comparison-in-java-1b7
Double comparison in Java - DEV Community
May 12, 2021 - Simply, we will subtract those doubles, make absolute value, and compare if the result is smaller than some very small number. double epsilon = 0.000001d; boolean result = Math.abs(firstDouble - secondDouble) < epsilon; System.out.println(result); ...
🌐
Medium
medium.com › @AlexanderObregon › javas-double-compare-method-explained-6159db37e326
Java’s Double.compare() Method Explained | Medium
December 23, 2024 - Learn how Java's Double.compare() method compares double values, handles NaN and infinity, and its use in sorting and data comparisons with examples.
🌐
HowToDoInJava
howtodoinjava.com › home › java examples › correct way to compare floats or doubles in java
Correct way to compare floats or doubles in Java
January 25, 2022 - Both methods are changing global var and are invoked twice. doubleOne results in 0.1*11+0.1*11, doubleTwo results in 0.1*11*11 You should: Either don’t change global vars in the methods, that’s the best. Invoke them once only, there is no need in invocation prior to if – remove those two method calls before the if block in main. Comments are closed. ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
🌐
GeeksforGeeks
geeksforgeeks.org › java › double-compare-method-in-java-with-examples
Double compare() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java Program to illustrate // the Double.compare() method import java.lang.Double; public class GFG { public static void main(String[] args) { // Get the two double values // to be compared Double d1 = 10d; Double d2 = 1023d; // function ...
Find elsewhere
🌐
Mtu
cclc.mtu.edu › java-doubles-and-floating-point-numbers
Java Doubles and Floating Point Numbers – CCLC
Another way to compare doubles is rounding. However, the more calculations, the greater the possibility that there will be larger rounding errors. Rounding is not always guaranteed to work. A third way to compare doubles is to use Java’s BigDecimal class to define floating point numbers.
🌐
Scaler
scaler.com › home › topics › java double compare()
Java Double compare() - Scaler Topics
May 4, 2023 - The compare() method in Java Double class is a static method used to compare two double values.
🌐
Educative
educative.io › answers › what-is-the-doublecompare-method-in-java
What is the Double.compare method in Java?
compare is a static method of the Double class which is used to compare two double values.
🌐
Delft Stack
delftstack.com › home › howto › java › compare doubles in java
How to Compare Doubles in Java | Delft Stack
March 11, 2025 - This method is particularly useful in applications where precision is crucial, such as in financial calculations. Java provides a built-in method called Double.compare() that can be used for comparing two doubles directly.
🌐
Javatpoint
javatpoint.com › java-double-compare-method
Java Double compare() Method - Javatpoint
Java Double.compareTo() Method The compareTo() method of Double class compares two double values numerically.
🌐
LabEx
labex.io › tutorials › java-how-to-compare-double-values-based-on-their-binary-format-in-java-414887
How to compare double values based on their binary format in Java | LabEx
This tutorial will guide you through the process of comparing double values in Java based on their binary format. Understanding the representation of double in Java and the nuances of floating-point comparison are crucial for accurate and reliable comparisons.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › double_compareto.htm
Java - Double compareTo() method
We're having two Double objects and using compareTo() method, we're comparing these double objects and then result is compared to 0. If result is greater than 0 then first number is greater than second number.
🌐
LabEx
labex.io › tutorials › java-java-double-equals-method-117605
Mastering the Java Double Equals Method | LabEx
In Java, the equals() method is used to compare the value of a Double object currently in use with the value of a parameter.
🌐
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › lang › Double.html
Double (Java SE 24 & JDK 24)
July 15, 2025 - This method imposes a total order on Double objects with two differences compared to the incomplete order defined by the Java language numerical comparison operators (<, <=, ==, >=, >) on double values.
🌐
DevGenius
blog.devgenius.io › double-comparison-in-java-d643f73ad7df
Double comparison in Java. Recently I was solving an interesting… | by Pavel Polívka | Dev Genius
May 12, 2021 - This inaccuracy is caused by rounding errors. We need to use a different approach to compare those doubles. If we do not have access to any libraries and want to solve this with Java only we can use something called the threshold method.
🌐
Caff
caff.de › posts › double_comparison
de·caff · Double Equality
February 10, 2023 - The standard solution in Java is falling back to java.lang.Double#compare(double, double).
🌐
Tutorialspoint
tutorialspoint.com › java › lang › double_equals.htm
Java - Double equals() method
The Java Double equals() method compares this object against the specified object. The result is true if and only if the argument is not null and is a Double object that represents a double that has the same value as the double represented by this