1 public static void main(String[] args) { 2 System.out.println(BigDecimal.ZERO); 3 String a = "-0.01"; 4 Double b = 10.00; 5 //new BigDecimal(0.01) Double类型转换有精度问题! 6 //BigDecimal val = new BigDecimal(0.01); 0.01000000000000000020816681711721685132943093776702880859375 7 BigDecimal val = new BigDecimal(b); 8 BigDecimal var = BigDecimal.valueOf(Double.valueOf(a)); 9 System.out.println(val); 10 System.out.println(var); 11 12 //true 13 if (val.equals(BigDecimal.TEN)){ 14 System.out.println("1"); 15 } 16 17 //signum()函数 判断 BigDecimal 的值 18 if(var.signum() == 1 ){ 19 //var 大于 0 返回 1 20 System.out.println("正数"); 21 } else if (var.signum() == 0){ 22 //var 等于 0 返回 0 23 System.out.println("0"); 24 } else if (var.signum() == -1 ){ 25 //var 小于 0 返回 -1 26 System.out.println("负数"); 27 } 28 29 //var 非正数 30 if (var.signum() != 1){ 31 System.out.println("非正数!"); 32 } 33 }
Bigdecimal的判断暂时用到这些,如果还有其他的请网友帮忙补充!