Java.lang.Float.compare() 方法

描述

java.lang.Float.compare() 方法比较两个指定的浮点值。 返回的整数值的符号与调用将返回的整数的符号相同 −

new Float(f1).compareTo(new Float(f2))


声明

以下是 java.lang.Float.compare() 方法的声明。

public static int compare(float f1, float f2)

参数

  • f1 − 这是要比较的第一个浮点数。

  • f2 − 这是要比较的第二个浮点数。


返回值

如果 f1 在数值上等于 f2,则此方法返回值 0; 如果 f1 在数值上小于 f2,则值小于 0; 如果 f1 在数值上大于 f2,则值大于 0。


异常

NA


示例

下面的例子展示了 java.lang.Float.compare() 方法的使用。

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");
      }
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

f1 is less than f2