Java Float - compare() Method
The java.lang.Float.compare() method is used to compare the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the call: new Float(f1).compareTo(new Float(f2)).
Syntax
public static int compare(float f1, float f2)
Parameters
f1 |
Specify the first float to compare. |
f2 |
Specify the second float to compare. |
Return Value
Returns the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2.
Exception
NA.
Example:
In the example below, the java.lang.Float.compare() method is used to compare given float values.
import java.lang.*; public class MyClass { public static void main(String[] args) { //creating float values float val1 = 5f; float val2 = 5f; float val3 = -5f; //comparing float values System.out.println("comparing val1 and val2: " + Float.compare(val1, val2)); System.out.println("comparing val1 and val3: " + Float.compare(val1, val3)); System.out.println("comparing val3 and val1: " + Float.compare(val3, val1)); } }
The output of the above code will be:
comparing val1 and val2: 0 comparing val1 and val3: 1 comparing val3 and val1: -1
❮ Java.lang - Float