Decimal Comparator
Write a method areEqualByThreeDecimalPlaces with two parameters of double
The method should return boolean and it needs to return true if two double decimal numbers are the same up to three decimal places. Otherwise, return false
Example
areEqualByThreeDeciamlPlaces(-3.125,-3.1259)-----> true
areEqualByThreeDeciamlPlaces(-3.125,-3.139)----> false
Soln: package com.cnc;
public class Main {
public static void main(String[] args) {
System.out.println(areEqualByThreeDecimalPlaces(-3.125, -3.1259));
}
public static boolean areEqualByThreeDecimalPlaces(double firstNumber, double secondNumber) {
if ((int) (firstNumber * 1000) == (int) (secondNumber * 1000)) {
return true;
}
return false;
}
}