This is a Java code that should test whether the first three digits after the decimal point are identical. And I need to write this program without a main method.
public class areEqualByThreeDecimalPlaces {
public static boolean areEqualByThreeDecimalPlaces(double d_1, double d_2) {
double value_1 = Math.round(1000*d_1);
double value_2 = Math.round(1000*d_2);
if (value_1 == value_2) {
return true;
} else return false;
}
}
Input:
(-3.1756, -3.175)
Expected Output:
true
Output received:
false
By using the Math.round
it is rounding the value of -3.1756
to -3.176
. But I want to check if the three digits after the decimal point are similar.
How do I correct my code?