为何要写这篇文章
在进行企业类型的数据运算,一般java的基本数据类型进行运算,难免有出现错误的情况,因此会采用java.math下的bigdemical,但是这个类的对数据作比较下有两种。因此要特意拿来记录一下。
Bigdecimal的数据比较
一开始,我采用的是equals去比较如下
Bigdecimal num1 = new Bigdecimal(0)
Bigdecimal num2 = new Bigdecimal(0.00)
if (num1.equals(0)) true
if (num1.equals(0)) false
此过程返回的是false,这样情况下0和0.00是不同的,原因是equals
因此采用以下方法来进行避免0和0.00的比较,一般是作除数的时候进行判断。以免出现空指针异常
if (num1.compareTo(BigDecimal.ZERO) == 0) {
}
Bigdecimal的除数运算
在进行Bigdecimal,假如进行除法运算时,会进行无限循环运算,为了避免此出现运算问题。一般采用BigDecimal自带的模式
Bigdecimal num1 = new Bigdecimal(10)
Bigdecimal num2 = new Bigdecimal(3)
BigDecimal result = num1.divide(num2, 2, BigDecimal.ROUND_HALF_DOWN);
// divide的第二个参数是保留位数
接下来是BigDecimal的静态参数,也是divide方法的第三个参数,具体应用的是对小数的保留位数的机制。
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a nonzero discarded fraction. Note that this rounding
* mode never decreases the magnitude of the calculated value.
*/
public final static int ROUND_UP = 0;
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*/
public final static int ROUND_DOWN = 1;
/**
* Rounding mode to round towards positive infinity. If the
* {@code BigDecimal} is positive, behaves as for
* {@code ROUND_UP}; if negative, behaves as for
* {@code ROUND_DOWN}. Note that this rounding mode never
* decreases the calculated value.
*/
public final static int ROUND_CEILING = 2;
/**
* Rounding mode to round towards negative infinity. If the
* {@code BigDecimal} is positive, behave as for
* {@code ROUND_DOWN}; if negative, behave as for
* {@code ROUND_UP}. Note that this rounding mode never
* increases the calculated value.
*/
public final static int ROUND_FLOOR = 3;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
public final static int ROUND_HALF_UP = 4;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal >} 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
public final static int ROUND_HALF_DOWN = 5;
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code ROUND_HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code ROUND_HALF_DOWN} if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
public final static int ROUND_HALF_EVEN = 6;
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*/
public final static int ROUND_UNNECESSARY = 7;
ROUND_HALF_DOWN模式就是1.5 -> 1
总结
在进行除法运算时,一般都是避免除数为零所报出异常。在进行比较的同时,也要对数据的格式做规范化。