Comparing those two values shall result in a "true":
53.9173333333333 53.9173
Comparing those two values shall result in a "true":
53.9173333333333 53.9173
If you want a = 1.00001
and b = 0.99999
be identified as equal:
return Math.abs(a - b) < 1e-4;
Otherwise, if you want a = 1.00010
and b = 1.00019
be identified as equal, and both a
and b
are positive and not huge:
return Math.floor(a * 10000) == Math.floor(b * 10000);
// compare by == is fine here because both sides are integral values.
// double can represent integral values below 2**53 exactly.
Otherwise, use the truncate
method as shown in Are there any functions for truncating a double in java?:
BigDecimal aa = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
aa = aa.setScale(4, BigDecimal.ROUND_DOWN);
bb = bb.setScale(4, BigDecimal.ROUND_DOWN);
return aa.equals(bb);
true
if double can represent integral values exactly. It's an obviously incorrect statement given the magnitudes doubles can work with. Then there's the fact that you shouldn't be using ==
on reference types like BigDecimal
, but that's an easy fix. –
Ligation BigDecimal
, this really does what is asked, making is very maintainable. This is what should be the preferred solution until performance has shown to need improvement. –
Wina here is the simple example if you still need this :)
public static boolean areEqualByThreeDecimalPlaces(double a, double b) {
a = a * 1000;
b = b * 1000;
int a1 = (int) a;
int b1 = (int) b;
if (a1 == b1) {
System.out.println("it works");
return true;
}
else
System.out.println("it doesn't work");
return false;
Naively:
if(Math.abs(a-b) < 0.0001)
However, that's not going to work correctly for all values. It's actually impossible to get it to work as long as you're using double
, because double
is implemented as binary fractons and does not even have decimal places.
You'll have to convert your values to String
or BigDecimal
to make meaningful tests about their decimal places.
You may want to read the Floating-Point Guide to improve your understanding of how floating point values work.
String
solution, but most of the times where such a comparation will occur, you just want to know it the results are "close enough", and in those scenarios, the line is pretty right. –
Yancey Apache commons has this: org.apache.commons.math3.util.Precision equals(double x, double y, double eps)
epsilon would be the distance you would allow. Looks like yours would be 1e-5?
The source-code of this method looks like it uses Math.abs as suggested in other answers.
public static boolean areEqualByThreeDecimalPlaces(double a, double b){
if(a < 0 && b < 0) {
double c = Math.ceil(a * 1000) / 1000;
double d = Math.ceil(b * 1000) / 1000;
return c == d;
}
if(a > 0 && b > 0){
double c = Math.floor(a * 1000) / 1000;
double d = Math.floor(b * 1000) / 1000;
return c == d;
}
return a == b;
}
public static boolean areEqualByThreeDecimalPlaces(double a, double b) {
if(a > 0 && b >0){
return ((int)Math.floor(a * 1000) == (int)Math.floor(b * 1000));
}
else if (a < 0 && b <0) {
return ((int)Math.ceil(a * 1000) == (int)Math.ceil(b * 1000));
}
return a==b;
}
Thanks. I did it this way:
double lon = 23.567889;
BigDecimal bdLon = new BigDecimal(lon);
bdLon = bdLon.setScale(4, BigDecimal.ROUND_HALF_UP);
System.out.println(bdLon.doubleValue());
public static boolean areEqualBySixDecimalPlaces(double a, double b) {
int num1 = (int) (a * 1e6);
int num2 = (int) (b * 1e6);
return num1 == num2;
}
Casting up to desired digits shall be the simplest solution if you know the values
Go ahead u can have a laugh over this one!!
public static boolean areEqualByThreeDecimalPlaces(double v, double v1) {
if((int)v == (int)v1){
String s = ""+v;
String s1 = ""+v1;
s = s.substring(s.indexOf(".")+1,s.indexOf(".")+4);
s1 = s1.substring(s1.indexOf(".")+1,s1.indexOf(".")+4);
return s.equals(s1);
}
return false;
}
public class DecimalComparator
{
public static boolean areEqualByThreeDecimalPlaces(double a, double b)
{
double threshold = 0.001; // This represents 1e-3 or 0.001
double diff = Math.abs(a - b);
return diff <threshold;
}
}
The solution to compare two double-digits up to three decimal places and return true or false might be:
public static boolean areEqualByThreeDecimalPlaces(double myFirstNumber , double mySecondNumber){
if (Math.abs(myFirstNumber - mySecondNumber) < 0.0009 ){
return true;
} else {
return false;
}
}
false
. You should replace the whole method body to return Math.abs(myFirstNumber - mySecondNumber) < 0.0009;
. –
Candlelight try this one
public static boolean isItEqual(double num1, double num2){
long firstDigit = (long)(num1 * 1000);
long secondDigit = (long)(num2 * 1000);
return firstDigit == secondDigit;
}
© 2022 - 2024 — McMap. All rights reserved.