BigDecimal比较相等,不能用equals,要用compareTo

BigDecimal比较2个值是否相等,不能用equals,而要用compareTo

1
2
3
4
5
6
7
8
public class BigDecimalTest {

public static void main(String[] args) {
BigDecimal b1 = BigDecimal.valueOf(-1);
BigDecimal b2 = BigDecimal.valueOf(-1.0);
System.out.print(b1.equals(b2)); //false
}
}
1
2
3
4
5
6
7
8
public class BigDecimalTest {

public static void main(String[] args) {
BigDecimal b1 = BigDecimal.valueOf(-1.0);
BigDecimal b2 = BigDecimal.valueOf(-1.0);
System.out.print(b1.equals(b2)); //true
}
}
1
2
3
4
5
6
7
8
public class BigDecimalTest {

public static void main(String[] args) {
BigDecimal b1 = BigDecimal.valueOf(-1);
BigDecimal b2 = BigDecimal.valueOf(-1.0);
System.out.print(b1.compareTo(b2) == 0); //true
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :