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

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

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

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

句法:

public static int compare(double a, double b)

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

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

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

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

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

下面的程序说明了 Double.compare() 方法:

示例 1:

// Java code to show implementation of
// Guava's Doubles.compare() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    public static void main(String[] args)
    {
        double a = 4.0;
        double b = 4.0;
  
        // compare method in Double class
        int output = Doubles.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 Doubles.compare() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    public static void main(String[] args)
    {
        double a = 5.6;
        double b = 4.2;
  
        // compare method in Double class
        int output = Doubles.compare(a, b);
  
        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}
输出:
Comparing 5.6 and 4.2 : 1

示例 3:

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