java.math.BigDecimal.compareTo(BigDecimal bg)方法檢查此BigDecimal和作為參數傳遞的BigDecimal對象bg是否相等。該方法將考慮兩個相等的BigDecimal對象,即使它們的值相等,而與小數位數無關。
用法:
public int compareTo(BigDecimal bg)
參數:此函數僅接受一個BigDecimal類型的BigDecimal對象,以便與此BigDecimal對象進行比較。
返回值:此方法可以返回以下值:
- 0:如果此BigDecimal的值等於作為參數傳遞的BigDecimal對象的值。
- 1:如果此BigDecimal的值大於作為參數傳遞的BigDecimal對象的值。
- -1:如果此BigDecimal的值小於作為參數傳遞的BigDecimal對象的值。
注意:該函數在比較124.0和124.0000時返回true,因為它不比較兩個BigDecimal對象的比例。
例子:
Input:b1 = new BigDecimal("4743.0008"), b2 = new BigDecimal("4743.00001") b1.compareTo(b2) Output:1 Input:b1 = new BigDecimal(4743), b2 = new BigDecimal("4743.00"); b1.compareTo(b2) Output:0
下麵的程序說明BigDecimal類的compareTo()函數的工作:
程序1:對於大於條件:
// Java program to demonstrate compareTo() method
import java.io.*;
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigDecimal objects
BigDecimal b1, b2;
b1 = new BigDecimal(67891);
b2 = new BigDecimal(12346);
if (b1.compareTo(b2) == 0) {
System.out.println(b1 + " and " + b2 + " are equal.");
}
else if (b1.compareTo(b2) == 1) {
System.out.println(b1 + " is greater than " + b2 + ".");
}
else {
System.out.println(b1 + " is lesser than " + b2 + ".");
}
}
}
輸出:
67891 is greater than 12346.
程序2:對於相等條件:
// Java program to demonstrate compareTo() method
import java.io.*;
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigDecimal objects
BigDecimal b1, b2;
b1 = new BigDecimal(67891);
b2 = new BigDecimal("67891.000");
if (b1.compareTo(b2) == 0) {
System.out.println(b1 + " and " + b2 + " are equal.");
}
else if (b1.compareTo(b2) == 1) {
System.out.println(b1 + " is greater than " + b2 + ".");
}
else {
System.out.println(b1 + " is lesser than " + b2 + ".");
}
}
}
輸出:
67891 and 67891.000 are equal.
程序3:對於少於條件:
// Java program to demonstrate compareTo() method
import java.io.*;
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigDecimal objects
BigDecimal b1, b2;
b1 = new BigDecimal("4743.00001");
b2 = new BigDecimal("4743.0008");
if (b1.compareTo(b2) == 0) {
System.out.println(b1 + " and " + b2 + " are equal.");
}
else if (b1.compareTo(b2) == 1) {
System.out.println(b1 + " is greater than " + b2 + ".");
}
else {
System.out.println(b1 + " is lesser than " + b2 + ".");
}
}
}
輸出:
4743.00001 is lesser than 4743.0008.
參考: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#compareTo(java.math.BigDecimal)
相關用法
- Java BigDecimal negate()用法及代碼示例
- Java CharBuffer compareTo()用法及代碼示例
- Java BigInteger compareTo()用法及代碼示例
- Java Integer compareTo()用法及代碼示例
- Java long compareTo()用法及代碼示例
- Java LocalDate compareTo()用法及代碼示例
- Java YearMonth compareTo()用法及代碼示例
- Java IntBuffer compareTo()用法及代碼示例
- Java Calendar compareTo()用法及代碼示例
- Java LocalTime compareTo()用法及代碼示例
- Java OffsetDateTime compareTo()用法及代碼示例
- Java ChronoZonedDateTime compareTo()用法及代碼示例
- Java MonthDay compareTo()用法及代碼示例
- Java OffsetTime compareTo()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 BigDecimal compareTo() Function in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。