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
相關用法
- Java Guava Doubles.min()用法及代碼示例
- Java Guava Doubles.contains()用法及代碼示例
- Java Guava Floats.contains()用法及代碼示例
- Java Guava Bytes.contains()用法及代碼示例
- Java Guava Longs.contains()用法及代碼示例
- Java Guava Shorts.min()用法及代碼示例
- Java Guava Longs.min()用法及代碼示例
- Java Guava Chars.min()用法及代碼示例
- Java Guava Floats.min()用法及代碼示例
- Java Guava Longs.max()用法及代碼示例
- Java Guava Doubles.max()用法及代碼示例
- Java Guava Chars.max()用法及代碼示例
- Java Guava Floats.max()用法及代碼示例
- Java Guava Booleans.contains()用法及代碼示例
- Java Guava Shorts.contains()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Java Guava | Doubles.compare() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。