I want to compare two numbers:
-3.123
-3.123456
I will use double
to store them and I just want to consider the first 3 decimals, so:
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalComparator {
public static void main(String[] args) {
areEqualByThreeDecimalPlaces(-3.123, -3.123456);
}
public static boolean areEqualByThreeDecimalPlaces (double one, double two) {
boolean same = true;
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.FLOOR);
System.out.println(df.format(one));
System.out.println(df.format(two));
if (df.format(one).equals(df.format(two))) {
same = true;
System.out.println("true");
} else {
same = false;
System.out.println("false");
}
return same;
}
}
The code is returning me:
-3.123
-3.124
false
why the second number is rounding to -3.124?