本文主要是介绍史上最全BigDecimal的5种进位方式:ROUND_UP,ROUND_DOWN,ROUND_CEILING,ROUND_FLOOR,ROUND_HALF_UP,ROUND_HALF_DOWN的比较,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先上JAVA官方文档
/*** 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;
看不懂别急,看翻译版本
/***舍入模式,从零开始舍入。总是增加*非零舍弃分数之前的数字。请注意,此舍入*模式从不降低计算值的大小。*/public final static int ROUND_UP = 0;/***舍入模式,向零舍入。从不递增数字*在丢弃的分数之前(即截断)。请注意*舍入模式从不增加计算值的大小。*/public final static int ROUND_DOWN = 1;/***舍入模式,向正无穷大舍入。如果*{@code BigDecimal}为正,行为与*{@code ROUND\u};如果为负,则行为与*{@code ROUND\u DOWN}。请注意,此舍入模式从不*减小计算值。*/public final static int ROUND_CEILING = 2;/***舍入模式,向负无穷大方向舍入。如果*{@code BigDecimal}为正,行为与*{@code ROUND\u DOWN};如果为负,则按*{@code ROUND\u}。请注意,此舍入模式从不*增加计算值。*/public final static int ROUND_FLOOR = 3;/***舍入模式向{@literal“nearest neighbor”舍入*除非两个邻居都是等距的,在这种情况下,就把他们围起来。*如果丢弃的分数是*≥0.5;否则,行为与{@code ROUND\u DOWN}相同。注意*这就是我们大多数人所学的四舍五入模式*小学。*/public final static int ROUND_HALF_UP = 4;/***舍入模式向{@literal“nearest neighbor”舍入*除非两个邻居是等距的,在这种情况下是圆的*趴下。如果丢弃*分数为{@literal>}0.5;否则,其行为与*{@code ROUND\u DOWN}。*/public final static int ROUND_HALF_DOWN = 5;
看到这里,除了最后两个翻译的,不懂之外,其他的,应该都清楚了,
这里解释一下最后两个的差别:
ROUND_HALF_UP: 遇到.5的情况时往上近似,例: 1.5 ->;2
ROUND_HALF_DOWN : 遇到.5的情况时往下近似,例: 1.5 ->;1
仅此而已
收工
这篇关于史上最全BigDecimal的5种进位方式:ROUND_UP,ROUND_DOWN,ROUND_CEILING,ROUND_FLOOR,ROUND_HALF_UP,ROUND_HALF_DOWN的比较的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!