写段代码,用一个数字(正数或负数),依次使用8种不同的模式,看i参数
public static void demo(BigDecimal bigDecimal, int scale){System.out.println();System.out.print(bigDecimal.toString()&#43;"\t");//循环使用8种舍入模式for (int i &#61;0 ;i<8;i&#43;&#43;){try{System.out.print(bigDecimal.setScale(scale,i)&#43;"\t");}catch (Exception e){System.out.printf(e.getMessage());}}}
然后尽可能造多个不同的数字&#xff0c;默认保留0位小数&#xff0c;即scale为0&#xff0c;依次调用上面的demo方法&#xff1a;
BigDecimal aa &#61; new BigDecimal("3.300");BigDecimal bb &#61; new BigDecimal("3.344");BigDecimal cc &#61; new BigDecimal("3.355");BigDecimal dd &#61; new BigDecimal("3.356");BigDecimal ee &#61; new BigDecimal("3.366");BigDecimal ff &#61; new BigDecimal("3.512");BigDecimal gg &#61; new BigDecimal("3.561");BigDecimal hh &#61; new BigDecimal("3.169");BigDecimal ii &#61; new BigDecimal("3.000");BigDecimal jj &#61; new BigDecimal("3.001");BigDecimal ll &#61; new BigDecimal("3.500");BigDecimal mm &#61; new BigDecimal("4.500");BigDecimal aa1 &#61; new BigDecimal("-3.300");BigDecimal bb1 &#61; new BigDecimal("-3.344");BigDecimal cc1 &#61; new BigDecimal("-3.355");BigDecimal dd1 &#61; new BigDecimal("-3.356");BigDecimal ee1 &#61; new BigDecimal("-3.366");BigDecimal ff1 &#61; new BigDecimal("-3.512");BigDecimal gg1 &#61; new BigDecimal("-3.561");BigDecimal hh1 &#61; new BigDecimal("-3.169");BigDecimal ii1 &#61; new BigDecimal("-3.000");BigDecimal jj1 &#61; new BigDecimal("-3.001");BigDecimal ll1 &#61; new BigDecimal("-3.500");BigDecimal mm1 &#61; 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);
控制台输出&#xff08;格式化后的&#xff09;&#xff1a;
1、正数&#xff1a;
值 ROUND_UP ROUND_DOWN ROUND_CEILING ROUND_FLOOR ROUND_HALF_UP ROUND_HALF_DOWN ROUND_HALF_EVEN ROUND_UNNECESSARY3.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
每种模式分别介绍下&#xff1a;
零、无论是正数还是负数都是往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 &#61; 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 &#61; 1;
二、区分正负数&#xff0c;正数变大&#xff0c;负数也是变大
/*** Rounding mode to round towards positive infinity. If the* {&#64;code BigDecimal} is positive, behaves as for* {&#64;code ROUND_UP}; if negative, behaves as for* {&#64;code ROUND_DOWN}. Note that this rounding mode never* decreases the calculated value.*/public final static int ROUND_CEILING &#61; 2;
三、区分正负数&#xff0c;正数变小&#xff0c;负数也是变小
/*** Rounding mode to round towards negative infinity. If the* {&#64;code BigDecimal} is positive, behave as for* {&#64;code ROUND_DOWN}; if negative, behave as for* {&#64;code ROUND_UP}. Note that this rounding mode never* increases the calculated value.*/public final static int ROUND_FLOOR &#61; 3;
四、正数小数位大于等于0.5则变大&#xff0c;负数小数位大于等于0.5则变小&#xff0c;注意这里是包含0.5
/*** Rounding mode to round towards {&#64;literal "nearest neighbor"}* unless both neighbors are equidistant, in which case round up.* Behaves as for {&#64;code ROUND_UP} if the discarded fraction is* ≥ 0.5; otherwise, behaves as for {&#64;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 &#61; 4;
五、正数小数位大于0.5则变大&#xff0c;负数小数位大于0.5则变小&#xff0c;注意这里是不包含0.5
/*** Rounding mode to round towards {&#64;literal "nearest neighbor"}* unless both neighbors are equidistant, in which case round* down. Behaves as for {&#64;code ROUND_UP} if the discarded* fraction is {&#64;literal >} 0.5; otherwise, behaves as for* {&#64;code ROUND_DOWN}.*/public final static int ROUND_HALF_DOWN &#61; 5;
六、此舍入模式也称为“银行家舍入法”&#xff0c;主要在美国使用。四舍六入&#xff0c;五分两种情况。如果前一位为奇数&#xff0c;则入位&#xff0c;否则舍去。
3.5->4但是4.5>4&#xff0c;因为3是奇数&#xff0c;4是偶数
/*** Rounding mode to round towards the {&#64;literal "nearest neighbor"}* unless both neighbors are equidistant, in which case, round* towards the even neighbor. Behaves as for* {&#64;code ROUND_HALF_UP} if the digit to the left of the* discarded fraction is odd; behaves as for* {&#64;code ROUND_HALF_DOWN} if it&#39;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 &#61; 6;
七、断言是有精度的&#xff0c;如果没有会报异常
/*** 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* {&#64;code ArithmeticException} is thrown.*/public final static int ROUND_UNNECESSARY &#61; 7;