Compare two Float value with compare method in Java
Description
The following code shows how to compare two Float value with compare method.
Example
//from w w w.j av a 2s . c o m
public class Main {
public static void main(String[] args) {
float f1 = 5.5f;
float f2 = 5.4f;
int i1 = Float.compare(f1, f2);
if (i1 > 0) {
System.out.println(">");
} else if (i1 < 0) {
System.out.println("<");
} else {
System.out.println("=");
}
Float fObj1 = new Float("5.5");
Float fObj2 = new Float("5.4");
int i2 = fObj1.compareTo(fObj2);
if (i2 > 0) {
System.out.println(">");
} else if (i2 < 0) {
System.out.println("<");
} else {
System.out.println("=");
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »