Created
August 1, 2019 11:28
-
-
Save 0ryant/79f0347dc6d1ec9c364749d448e09f90 to your computer and use it in GitHub Desktop.
Java - Coding Challenge 5 - Decimal Comparator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// numbers must be equal up to 3 decimal places | |
public class DecimalComparator { | |
public static boolean areEqualByThreeDecimalPlaces(double first,double second) { | |
if (first <0 && second >0 || first >0 && second <0){ | |
return false; | |
} | |
double firstThousand = first*1000; | |
double secondThousand = second*1000; | |
int firstint = (int)firstThousand; | |
int secondint = (int)secondThousand; | |
if (firstint==secondint){ | |
return true; | |
} | |
return false; | |
} | |
} |
It needs to return true if two double numbers are the same up to three decimal places. Otherwise, returns false.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can some body explainme about this
package edu.fcc.cmis.decimalcomparator;
public class DecimalComparator {
// System.out.println(sameIntegralPart(22.34, 22.37867)); // true
// System.out.println(sameIntegralPart(141457105122.34667, 141457105111.34667)); // false
//
// System.out.println(areEqualWithinPrecision(-1341.235, -1341.23534667, 3)); // true
// System.out.println(areEqualWithinPrecision(1341.2357, 1341.235346, 4)); // false
// System.out.println(areEqualWithinPrecision(0.2357, 0.235346, 4)); // false
//
// System.out.println(areEqualWithinPrecision(0.2357, 0.235346)); // true
}
}
Footer