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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。