📌  相关文章
📜  Java番石榴 |带有示例的 Floats.compare() 方法

📅  最后修改于: 2022-05-13 01:54:58.395000             🧑  作者: Mango

Java番石榴 |带有示例的 Floats.compare() 方法

Floats 类的Floats.compare()方法用于比较两个指定的浮点值。这些值作为参数传递,比较结果作为第一个值和第二个值的差值。因此,它可以是正数、零或负数。

句法:

public static int compare(float a, float b)

参数:此方法接受两个参数:

  • a:这是要比较的第一个浮点对象。
  • b:这是要比较的第二个浮点对象。

返回类型:此方法返回一个 int 值。它返回:

  • 0 如果“a”等于“b”,
  • 正值“a”大于“b”,
  • 负值“a”小于“b”

异常:该方法不抛出任何异常。

示例 1:

// Java code to show implementation of
// Guava's Floats.compare() method
import com.google.common.primitives.Floats;
  
class GFG {
    public static void main(String[] args)
    {
        float a = 4f;
        float b = 4f;
  
        // compare method in Float class
        int output = Floats.compare(a, b);
  
        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}
输出:
Comparing 4.0 and 4.0 : 0

示例 2:

// Java code to show implementation of
// Guava's Floats.compare() method
import com.google.common.primitives.Floats;
  
class GFG {
    public static void main(String[] args)
    {
        float a = 4.2f;
        float b = 3.1f;
  
        // compare method in Float class
        int output = Floats.compare(a, b);
  
        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}
输出:
Comparing 4.2 and 3.1 : 1

示例 3:

// Java code to show implementation of
// Guava's Floats.compare() method
import com.google.common.primitives.Floats;
  
class GFG {
    public static void main(String[] args)
    {
        float a = 2.5f;
        float b = 4f;
  
        // compare method in Float class
        int output = Floats.compare(a, b);
  
        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}
输出:
Comparing 2.5 and 4.0 : -1