目录

static int compare(float f1, float f2)

描述 (Description)

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

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

声明 (Declaration)

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

public static int compare(float f1, float f2)

参数 (Parameters)

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

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

返回值 (Return Value)

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

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.lang.Float.compare()方法的用法。

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018