当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Guava Doubles.compare()用法及代码示例


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

用法:

public static int compare(double a, double b)

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


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

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

  • 如果“ a”等于“ b”,则为0,
  • 如果“ 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


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Java Guava | Doubles.compare() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。