Compare two double values with Double.doubleToLongBits in Java
Description
The following code shows how to compare two double values with Double.doubleToLongBits.
Example
//from w w w . ja va 2 s . c om
public class Main {
static public boolean areEqual(double aThis, double aThat) {
return Double.doubleToLongBits(aThis) == Double.doubleToLongBits(aThat);
}
public static void main(String[] argv){
System.out.println(areEqual(1.2,2.1));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »