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 OverflowHow do you compare two double values
Double vs BigDecimal in financial programming
Use long or big decimal. Performance problems will most probably arise elsewhere before you factor a big decimal as the culprit
More on reddit.comHow to compare two Java objects?
Java: Comparing two numbers of type double to three decimal places using a boolean value
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) {...}
Instead of using doubles for decimal arithemetic, please use java.math.BigDecimal. It would produce the expected results.
For reference take a look at this stackoverflow question
How do you compare two double values in C programming?
for Example:
double a = .846;
double b = .655 ;
I know that I can not treat them as an int and do the following
if(a > b){
print - a is larger
} else{
print - b is larger
}
But how should I solve it?