写段代码,用一个数字(正数或负数),依次使用8种不同的模式,看i参数
public static void demo(BigDecimal bigDecimal, int scale){
System.out.println();
System.out.print(bigDecimal.toString()+"\t");
//循环使用8种舍入模式
for (int i =0 ;i<8;i++){
try{
System.out.print(bigDecimal.setScale(scale,i)+"\t");
}catch (Exception e){
System.out.printf(e.getMessage());
}
}
}
然后尽可能造多个不同的数字,默认保留0位小数,即scale为0,依次调用上面的demo方法:
BigDecimal aa = new BigDecimal("3.300");
BigDecimal bb = new BigDecimal("3.344");
BigDecimal cc = new BigDecimal("3.355");
BigDecimal dd = new BigDecimal("3.356");
BigDecimal ee = new BigDecimal("3.366");
BigDecimal ff = new BigDecimal("3.512");
BigDecimal gg = new BigDecimal("3.561");
BigDecimal hh = new BigDecimal("3.169");
BigDecimal ii = new BigDecimal("3.000");
BigDecimal jj = new BigDecimal("3.001");
BigDecimal ll = new BigDecimal("3.500");
BigDecimal mm = new BigDecimal("4.500");
BigDecimal aa1 = new BigDecimal("-3.300");
BigDecimal bb1 = new BigDecimal("-3.344");
BigDecimal cc1 = new BigDecimal("-3.355");
BigDecimal dd1 = new BigDecimal("-3.356");
BigDecimal ee1 = new BigDecimal("-3.366");
BigDecimal ff1 = new BigDecimal("-3.512");
BigDecimal gg1 = new BigDecimal("-3.561");
BigDecimal hh1 = new BigDecimal("-3.169");
BigDecimal ii1 = new BigDecimal("-3.000");
BigDecimal jj1 = new BigDecimal("-3.001");
BigDecimal ll1 = new BigDecimal("-3.500");
BigDecimal mm1 = new BigDecimal("-4.500");
demo(aa,0);
demo(bb,0);
demo(cc,0);
demo(dd,0);
demo(ee,0);
demo(ff,0);
demo(gg,0);
demo(hh,0);
demo(ii,0);
demo(jj,0);
demo(ll,0);
demo(mm,0);
System.out.println();
System.out.println("------------------------------------------------------");
demo(aa1,0);
demo(bb1,0);
demo(cc1,0);
demo(dd1,0);
demo(ee1,0);
demo(ff1,0);
demo(gg1,0);
demo(hh1,0);
demo(ii1,0);
demo(jj1,0);
demo(ll1,0);
demo(mm1,0);
控制台输出(格式化后的):
1、正数:
值 ROUND_UP ROUND_DOWN ROUND_CEILING ROUND_FLOOR ROUND_HALF_UP ROUND_HALF_DOWN ROUND_HALF_EVEN ROUND_UNNECESSARY
3.300 4 3 4 3 3 3 3 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.344 4 3 4 3 3 3 3 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.355 4 3 4 3 3 3 3 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.356 4 3 4 3 3 3 3 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.366 4 3 4 3 3 3 3 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.512 4 3 4 3 4 4 4 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.561 4 3 4 3 4 4 4 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.169 4 3 4 3 3 3 3 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.000 3 3 3 3 3 3 3 3
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.001 4 3 4 3 3 3 3 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.500 4 3 4 3 4 3 4 Rounding necessary
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
4.500 5 4 5 4 5 4 4 Rounding necessary
2、负数
值 ROUND_UP ROUND_DOWN ROUND_CEILING ROUND_FLOOR ROUND_HALF_UP ROUND_HALF_DOWN ROUND_HALF_EVEN ROUND_UNNECESSARY
-3.300 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.344 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.355 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.356 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.366 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.512 -4 -3 -3 -4 -4 -4 -4 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.561 -4 -3 -3 -4 -4 -4 -4 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.169 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.000 -3 -3 -3 -3 -3 -3 -3 -3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.001 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.500 -4 -3 -3 -4 -4 -3 -4 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-4.500 -5 -4 -4 -5 -5 -4 -4 Rounding necessary
每种模式分别介绍下:
零、无论是正数还是负数都是往0以外的方向
/**
* 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;
一、无论是正数还是负数都是往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;
四、正数小数位大于等于0.5则变大,负数小数位大于等于0.5则变小,注意这里是包含0.5
/**
* 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;
五、正数小数位大于0.5则变大,负数小数位大于0.5则变小,注意这里是不包含0.5
/**
* 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;
六、此舍入模式也称为“银行家舍入法”,主要在美国使用。四舍六入,五分两种情况。如果前一位为奇数,则入位,否则舍去。
3.5->4但是4.5>4,因为3是奇数,4是偶数
/**
* 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;