Java - Float compare() method



Description

The Java Float compare() method compares 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))

Declaration

Following is the declaration for java.lang.Float.compare() method

public static int compare(float f1, float f2)

Parameters

  • f1 − This is the first float to compare

  • f2 − This is the second float to compare.

Return Value

This method 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 1

The following example shows the usage of Float compare() method to check a value is greater than another value or not. We're having two float values and using compare() method, we're comparing these float values and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {

      // compares the two specified float values
      float f1 = 15.45f;
      float f2 = 11.50f;
      int retval = Float.compare(f1, f2);
    
      if(retval > 0) {
         System.out.println("f1 is greater than f2");
      } else if(retval < 0) {
        System.out.println("f1 is less than f2");
      } else {
         System.out.println("f1 is equal to f2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

f1 is greater than f2

Example 2

The following is another example to show the usage of Float compare() method to check a value is less than another value or not. We're having two float values and using compare() method, we're comparing these float values and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {

      // compares the two specified float values
      float f1 = 11.50f;
      float f2 = 15.45f;
      int retval = Float.compare(f1, f2);
    
      if(retval > 0) {
         System.out.println("f1 is greater than f2");
      } else if(retval < 0) {
        System.out.println("f1 is less than f2");
      } else {
         System.out.println("f1 is equal to f2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

f1 is less than f2

Example 3

The following example shows the usage of Float compare() method to check a value is same as another value or not. We're having two float values and using compare() method, we're comparing these float values and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {

      // compares the two specified float values
      float f1 = 15.45f;
      float f2 = 15.45f;
      int retval = Float.compare(f1, f2);
    
      if(retval > 0) {
         System.out.println("f1 is greater than f2");
      } else if(retval < 0) {
        System.out.println("f1 is less than f2");
      } else {
         System.out.println("f1 is equal to f2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

f1 is equal to f2
java_lang_float.htm
Advertisements