BigDecimal类型比较数字大小
1.转成int
-
BigDecimal b1 = new BigDecimal("-121454125453.145");
-
if(b1.intValue()<0) {
-
System.out.println("金额为负数!");
-
System.out.println(b1);
-
}
2.compareTo方法,
结果是: -1:小于; 0 :等于; 1 :大于;
-
BigDecimal b1 = new BigDecimal("-121454125453.145");
-
if(b1.compareTo(BigDecimal.ZERO)==-1) {
-
System.out.println("金额为负数!");
-
}
BigDecimal运算
10-19
BigDecimal运算Jar包。支持sin、cos、tan、asin、acos、atan、pow、sqrt、cbrt、root、log10、log、ln、exp、sinh、cosh、tanh、asi
java BigDecimal类型 比较大小
7769
一般我们进行类型比较有.equals()、==、.compareTo()但是当比较bigdecimal类型时最好使用.compareTo()来比较大小BigDecimal a=BigDecimal.ZERO;BigDecimal b=BigDecimal.valueOf(0.000);BigDecimal c=BigDecimal.valueOf(0.0);a.equals(b) 的结果会是fal...
public int compareTo(BigDecimal val) {
// Quick path for equal scale and non-inflated case.
if (scale == val.scale) {
long xs = intCompact;
long ys = val.intCompact;
if (xs != INFLATED && ys != INFLATED)
return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
}
int xsign = this.signum();
int ysign = val.signum();
if (xsign != ysign)
return (xsign > ysign) ? 1 : -1;
if (xsign == 0)
return 0;
int cmp = compareMagnitude(val);
return (xsign > 0) ? cmp : -cmp;
}