BigDecimal 的加減乘除和比較

2021-06-01 13:57:09 字數 1319 閱讀 4794

bigdecimal childrate = new bigdecimal(

accountinfodto.getchildmchtrate() == null ? "0"

: accountinfodto.getchildmchtrate());

bigdecimal grouprate = new bigdecimal(

accountinfodto.getgroupmchtrate() == null ? "0"

: accountinfodto.getgroupmchtrate());

加: bigdecimal groupandchildrate = childrate.add(grouprate);

減:bigdecimal groupratenew = new bigdecimal("100").subtract(childrate);(注意被除數不可以為0)

乘:bigdecimal groupratenew  = childrate.multiply(grouprate);

除:childrate = childrate.divide((groupandchildrate), 2,bigdecimal.round_half_up));

餘數的幾位小數的捨入方式(

round_ceiling :向正無窮方向捨入

round_down :向零方向捨入

round_floor :向負無窮方向捨入

round_half_down :向(距離)最近的一邊捨入,除非兩邊(的距離)是相等,如果是這樣,向下捨入, 例如1.55 保留一位小數結果為1.5

round_half_even :

向(距離)最近的一邊捨入,除非兩邊(的距離)是相等,如果是這樣,如果保留位數是奇數,使用round_half_up ,如果是偶數,使用round_half_down

round_half_up :向(距離)最近的一邊捨入,除非兩邊(的距離)是相等,如果是這樣,向上捨入, 1.55保留一位小數結果為1.6

round_unnecessary :計算結果是精確的,不需要捨入模式

round_up: 向遠離0的方向捨入 

比較:    if (childrate.compareto(new bigdecimal("0")) == 0)

bigdecimal 的值進行四捨五入:

(new bigdecimal(dto.getrulevalue())).multiply(new bigdecimal(100)).setscale(2,bigdecimal.round_half_up):表示乘出來後進行四捨五入小數點後兩位。(bigdecimal.round_half_up:四捨五入)

BigDecimal 加 減 乘 除 比較

bigdecimal 多用於商業計算,或者較大數值得計算 bigdecimal bignum1 newbigdecimal 10 bigdecimal bignum2 new bigdecimal 5 bigdecimal bignum3 null 加法 bignum3 bignum1.add bi...

BigDecimal加減乘除計算

bigdecimal num1 new bigdecimal 0.005 bigdecimal num2 new bigdecimal 1000000 bigdecimal num3 new bigdecimal 1000000 盡量用字串的形式初始化 bigdecimal num12 new bi...

BigDecimal的運算 加減乘除

一 建立乙個bigdecimal物件。這裡對比了兩種形式,第一種直接value寫數字的值,第二種用string來表示 bigdecimal num1 new bigdecimal 0.005 bigdecimal num2 new bigdecimal 1000000 bigdecimal num3...