java开发150个建议(20)

Summary of Rounding Operations Under Different Rounding Modes28* Result of rounding input to one digit with the given29*rounding mode30* 31* Input Number{@code UP}32*{@code DOWN}33*{@code CEILING}34*{@code FLOOR}35*{@code HALF_UP}36*{@code HALF_DOWN}37*{@code HALF_EVEN}38*{@code UNNECESSARY}39*40* 5.56565656throw {@code ArithmeticException}41* 2.53232322throw {@code ArithmeticException}42* 1.62121222throw {@code ArithmeticException}43* 1.12121111throw {@code ArithmeticException}44* 1.01111111145* -1.0 -1 -1-1-1 -1-1-1-146* -1.1 -2 -1-1-2 -1-1-1throw {@code ArithmeticException}47* -1.6 -2 -1-1-2 -2-2-2throw {@code ArithmeticException}48* -2.5 -3 -2-2-3 -3-2-2throw {@code ArithmeticException}49* -5.5 -6 -5-5-6 -6-5-6throw {@code ArithmeticException}50*51*52*53* This {@code enum} is intended to replace the integer-based54* enumeration of rounding mode constants in {@link BigDecimal}55* ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},56* etc. ).57*58* @seeBigDecimal59* @seeMathContext60* @authorJosh Bloch61* @authorMike Cowlishaw62* @authorJoseph D. Darcy63* @since 1.564*/65 public enum RoundingMode {66 67/**68* Rounding mode to round away from zero.Always increments the69* digit prior to a non-zero discarded fraction.Note that this70* rounding mode never decreases the magnitude of the calculated71* value.72*73*Example:74*75*Input Number76*Input rounded to one digit
with {@code UP} rounding77*5.5678*2.5379*1.6280*1.1281*1.0182*-1.0 -183*-1.1 -284*-1.6 -285*-2.5 -386*-5.5 -687*88*/89UP(BigDecimal.ROUND_UP),90 91/**92* Rounding mode to round towards zero.Never increments the digit93* prior to a discarded fraction (i.e., truncates).Note that this94* rounding mode never increases the magnitude of the calculated value.95*96*Example:97*98*Input Number99*Input rounded to one digit
with {@code DOWN} rounding100*5.55101*2.52102*1.61103*1.11104*1.01105*-1.0 -1106*-1.1 -1107*-1.6 -1108*-2.5 -2109*-5.5 -5110*111*/112DOWN(BigDecimal.ROUND_DOWN),113 114/**115* Rounding mode to round towards positive infinity.If the116* result is positive, behaves as for {@code RoundingMode.UP};117* if negative, behaves as for {@code RoundingMode.DOWN}.Note118* that this rounding mode never decreases the calculated value.119*120*Example:121*122*Input Number123*Input rounded to one digit
with {@code CEILING} rounding124*5.56125*2.53126*1.62127*1.12128*1.01129*-1.0 -1130*-1.1 -1131*-1.6 -1132*-2.5 -2133*-5.5 -5134*135*/136CEILING(BigDecimal.ROUND_CEILING),137 138/**139* Rounding mode to round towards negative infinity.If the140* result is positive, behave as for {@code RoundingMode.DOWN};141* if negative, behave as for {@code RoundingMode.UP}.Note that142* this rounding mode never increases the calculated value.143*144*Example:145*146*Input Number147*Input rounded to one digit
with {@code FLOOR} rounding148*5.55149*2.52150*1.61151*1.11152*1.01153*-1.0 -1154*-1.1 -2155*-1.6 -2156*-2.5 -3157*-5.5 -6158*159*/160FLOOR(BigDecimal.ROUND_FLOOR),161 162/**163* Rounding mode to round towards {@literal "nearest neighbor"}164* unless both neighbors are equidistant, in which case round up.165* Behaves as for {@code RoundingMode.UP} if the discarded166* fraction is ≥ 0.5; otherwise, behaves as for167* {@code RoundingMode.DOWN}.Note that this is the rounding168* mode commonly taught at school.169*170*Example:171*172*Input Number173*Input rounded to one digit
with {@code HALF_UP} rounding174*5.56175*2.53176*1.62177*1.11178*1.01179*-1.0 -1180*-1.1 -1181*-1.6 -2182*-2.5 -3183*-5.5 -6184*185*/186HALF_UP(BigDecimal.ROUND_HALF_UP),187 188/**189* Rounding mode to round towards {@literal "nearest neighbor"}190* unless both neighbors are equidistant, in which case round191* down.Behaves as for {@code RoundingMode.UP} if the discarded192* fraction is > 0.5; otherwise, behaves as for193* {@code RoundingMode.DOWN}.194*195*Example:196*197*Input Number198*Input rounded to one digit
with {@code HALF_DOWN} rounding199*5.55200*2.52201*1.62202*1.11203*1.01204*-1.0 -1205*-1.1 -1206*-1.6 -2207*-2.5 -2208*-5.5 -5209*210*/211HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),212 213/**214* Rounding mode to round towards the {@literal "nearest neighbor"}215* unless both neighbors are equidistant, in which case, round216* towards the even neighbor.Behaves as for217* {@code RoundingMode.HALF_UP} if the digit to the left of the218* discarded fraction is odd; behaves as for219* {@code RoundingMode.HALF_DOWN} if it's even.Note that this220* is the rounding mode that statistically minimizes cumulative221* error when applied repeatedly over a sequence of calculations.222* It is sometimes known as {@literal "Banker's rounding,"} and is223* chiefly used in the USA.This rounding mode is analogous to224* the rounding policy used for {@code float} and {@code double}225* arithmetic in Java.226*227*Example:228*229*Input Number230*Input rounded to one digit