Java.lang.Float.compare() Method

Advertisements


Description

The java.lang.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

The following example shows the usage of java.lang.Float.compare() method.

package com.tutorialspoint;

import java.lang.*;

public class FloatDemo {

   public static void main(String[] args) {

     // compares the two specified float values
     float f1 = 22.30f;
     float f2 = 88.67f;
     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");
     }
   }
}   

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

f1 is less than f2



Advertisements
Advertisements