📜  Java中的 BigDecimal compareTo()函数

📅  最后修改于: 2022-05-13 01:55:04.770000             🧑  作者: Mango

Java中的 BigDecimal compareTo()函数

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: Java Java.math.BigDecimal)