比较方法:
BigDecimal类提供compareTo()方法来比较两个数的大小,例如:a = b返回0,a < b返回-1,a > b返回1,通过这三种比较返回的结果,我们还可以比较a != b、a >= b和a<= b这三种情况。
示例代码:
import java.math.BigDecimal;public class Application { public static void main(String[] args) { BigDecimal a = new BigDecimal(10); BigDecimal b = new BigDecimal(5); if (a.compareTo(b) == 0) System.out.println("a = b"); if (a.compareTo(b) == -1) System.out.println("a < b"); if (a.compareTo(b) == 1) System.out.println("a > b"); if (a.compareTo(b) != 0) System.out.println("a != b"); if (a.compareTo(b) != -1) System.out.println("a >= b"); if (a.compareTo(b) != 1) System.out.println("a <= b"); }}