Float.compare() in Java

Float.compare(): This method is available in java.lang.Double class of Java.

Syntax:

int java.lang.Float.compare(float f1, float f2)

This method takes two arguments of type float as its parameters. This method compares the two specified float values.

Parameters: Two parameters are required for this method.

f1: the first float to compare.

f2: the second float to compare.

Returns:

1. The value 0 if f1 is numerically equal to f2.

2. a value less than 0 if f1 is numerically less than  f2.

3. A value greater than 0if f1 is numerically greater than f2.

For Example:

float f1 = (float) 10.8, f2 = (float) 17.9

Float.compare(f1,f2) = > It returns -1.

Approach

Java

public class FloatCompare {
    public static void main(String[] args) {

        float f1 = (float10.8, f2 = (float17.9;
        System.out.println(Float.compare(f1, f2));

    }
}

Output:

-1

No comments:

Post a Comment