Floats類的Floats.compare()方法用於比較兩個指定的float值。這些值作為參數傳遞,比較結果作為第一值和第二值之差得到。因此,它可以為正,零或負。
用法:
public static int compare(float a, float b)
參數:此方法接受兩個參數:
- a:這是要比較的第一個float對象。
- b:這是要比較的第二個float對象。
返回類型:此方法返回一個int值。它返回:
- 如果“ a”等於“ b”,則為0,
- 正值“ 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
相關用法
- 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 | Floats.compare() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。