34 * the least significant returned digit of a rounded result is to be 35 * calculated. If fewer digits are returned than the digits needed to 36 * represent the exact numerical result, the discarded digits will be 37 * referred to as the <i>discarded fraction</i> regardless the digits' 38 * contribution to the value of the number. In other words, 39 * considered as a numerical value, the discarded fraction could have 40 * an absolute value greater than one. 41 * 42 * <p>Each rounding mode description includes a table listing how 43 * different two-digit decimal values would round to a one digit 44 * decimal value under the rounding mode in question. The result 45 * column in the tables could be gotten by creating a 46 * {@code BigDecimal} number with the specified value, forming a 47 * {@link MathContext} object with the proper settings 48 * ({@code precision} set to {@code 1}, and the 49 * {@code roundingMode} set to the rounding mode in question), and 50 * calling {@link BigDecimal#round round} on this number with the 51 * proper {@code MathContext}. A summary table showing the results 52 * of these rounding operations for all rounding modes appears below. 53 * 54 *<table border> 55 * <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption> 56 * <tr><th></th><th colspan=8>Result of rounding input to one digit with the given 57 * rounding mode</th> 58 * <tr style="vertical-align:top"> 59 * <th>Input Number</th> <th>{@code UP}</th> 60 * <th>{@code DOWN}</th> 61 * <th>{@code CEILING}</th> 62 * <th>{@code FLOOR}</th> 63 * <th>{@code HALF_UP}</th> 64 * <th>{@code HALF_DOWN}</th> 65 * <th>{@code HALF_EVEN}</th> 66 * <th>{@code UNNECESSARY}</th> 67 * 68 * <tr style="text-align:right"><td>5.5</td> <td>6</td> <td>5</td> <td>6</td> <td>5</td> <td>6</td> <td>5</td> <td>6</td> <td>throw {@code ArithmeticException}</td> 69 * <tr style="text-align:right"><td>2.5</td> <td>3</td> <td>2</td> <td>3</td> <td>2</td> <td>3</td> <td>2</td> <td>2</td> <td>throw {@code ArithmeticException}</td> 70 * <tr style="text-align:right"><td>1.6</td> <td>2</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>throw {@code ArithmeticException}</td> 71 * <tr style="text-align:right"><td>1.1</td> <td>2</td> <td>1</td> <td>2</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>throw {@code ArithmeticException}</td> 72 * <tr style="text-align:right"><td>1.0</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> 73 * <tr style="text-align:right"><td>-1.0</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> 74 * <tr style="text-align:right"><td>-1.1</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>throw {@code ArithmeticException}</td> 75 * <tr style="text-align:right"><td>-1.6</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-2</td> <td>-2</td> <td>-2</td> <td>-2</td> <td>throw {@code ArithmeticException}</td> 76 * <tr style="text-align:right"><td>-2.5</td> <td>-3</td> <td>-2</td> <td>-2</td> <td>-3</td> <td>-3</td> <td>-2</td> <td>-2</td> <td>throw {@code ArithmeticException}</td> 77 * <tr style="text-align:right"><td>-5.5</td> <td>-6</td> <td>-5</td> <td>-5</td> <td>-6</td> <td>-6</td> <td>-5</td> <td>-6</td> <td>throw {@code ArithmeticException}</td> 78 *</table> 79 * 80 * 81 * <p>This {@code enum} is intended to replace the integer-based 82 * enumeration of rounding mode constants in {@link BigDecimal} 83 * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN}, 84 * etc. ). 85 * 86 * @see BigDecimal 87 * @see MathContext 88 * @author Josh Bloch 89 * @author Mike Cowlishaw 90 * @author Joseph D. Darcy 91 * @since 1.5 92 */ 93 @SuppressWarnings("deprecation") // Legacy rounding mode constants in BigDecimal 94 public enum RoundingMode { 95 96 /** 97 * Rounding mode to round away from zero. Always increments the 98 * digit prior to a non-zero discarded fraction. Note that this 99 * rounding mode never decreases the magnitude of the calculated 100 * value. 101 * 102 *<p>Example: 103 *<table border> 104 * <caption><b>Rounding mode UP Examples</b></caption> 105 *<tr style="vertical-align:top"><th>Input Number</th> 106 * <th>Input rounded to one digit<br> with {@code UP} rounding 107 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 108 *<tr style="text-align:right"><td>2.5</td> <td>3</td> 109 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 110 *<tr style="text-align:right"><td>1.1</td> <td>2</td> 111 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 112 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 113 *<tr style="text-align:right"><td>-1.1</td> <td>-2</td> 114 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 115 *<tr style="text-align:right"><td>-2.5</td> <td>-3</td> 116 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 117 *</table> 118 */ 119 UP(BigDecimal.ROUND_UP), 120 121 /** 122 * Rounding mode to round towards zero. Never increments the digit 123 * prior to a discarded fraction (i.e., truncates). Note that this 124 * rounding mode never increases the magnitude of the calculated value. 125 * 126 *<p>Example: 127 *<table border> 128 * <caption><b>Rounding mode DOWN Examples</b></caption> 129 *<tr style="vertical-align:top"><th>Input Number</th> 130 * <th>Input rounded to one digit<br> with {@code DOWN} rounding 131 *<tr style="text-align:right"><td>5.5</td> <td>5</td> 132 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 133 *<tr style="text-align:right"><td>1.6</td> <td>1</td> 134 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 135 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 136 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 137 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 138 *<tr style="text-align:right"><td>-1.6</td> <td>-1</td> 139 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 140 *<tr style="text-align:right"><td>-5.5</td> <td>-5</td> 141 *</table> 142 */ 143 DOWN(BigDecimal.ROUND_DOWN), 144 145 /** 146 * Rounding mode to round towards positive infinity. If the 147 * result is positive, behaves as for {@code RoundingMode.UP}; 148 * if negative, behaves as for {@code RoundingMode.DOWN}. Note 149 * that this rounding mode never decreases the calculated value. 150 * 151 *<p>Example: 152 *<table border> 153 * <caption><b>Rounding mode CEILING Examples</b></caption> 154 *<tr style="vertical-align:top"><th>Input Number</th> 155 * <th>Input rounded to one digit<br> with {@code CEILING} rounding 156 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 157 *<tr style="text-align:right"><td>2.5</td> <td>3</td> 158 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 159 *<tr style="text-align:right"><td>1.1</td> <td>2</td> 160 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 161 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 162 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 163 *<tr style="text-align:right"><td>-1.6</td> <td>-1</td> 164 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 165 *<tr style="text-align:right"><td>-5.5</td> <td>-5</td> 166 *</table> 167 */ 168 CEILING(BigDecimal.ROUND_CEILING), 169 170 /** 171 * Rounding mode to round towards negative infinity. If the 172 * result is positive, behave as for {@code RoundingMode.DOWN}; 173 * if negative, behave as for {@code RoundingMode.UP}. Note that 174 * this rounding mode never increases the calculated value. 175 * 176 *<p>Example: 177 *<table border> 178 * <caption><b>Rounding mode FLOOR Examples</b></caption> 179 *<tr style="vertical-align:top"><th>Input Number</th> 180 * <th>Input rounded to one digit<br> with {@code FLOOR} rounding 181 *<tr style="text-align:right"><td>5.5</td> <td>5</td> 182 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 183 *<tr style="text-align:right"><td>1.6</td> <td>1</td> 184 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 185 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 186 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 187 *<tr style="text-align:right"><td>-1.1</td> <td>-2</td> 188 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 189 *<tr style="text-align:right"><td>-2.5</td> <td>-3</td> 190 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 191 *</table> 192 */ 193 FLOOR(BigDecimal.ROUND_FLOOR), 194 195 /** 196 * Rounding mode to round towards {@literal "nearest neighbor"} 197 * unless both neighbors are equidistant, in which case round up. 198 * Behaves as for {@code RoundingMode.UP} if the discarded 199 * fraction is ≥ 0.5; otherwise, behaves as for 200 * {@code RoundingMode.DOWN}. Note that this is the rounding 201 * mode commonly taught at school. 202 * 203 *<p>Example: 204 *<table border> 205 * <caption><b>Rounding mode HALF_UP Examples</b></caption> 206 *<tr style="vertical-align:top"><th>Input Number</th> 207 * <th>Input rounded to one digit<br> with {@code HALF_UP} rounding 208 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 209 *<tr style="text-align:right"><td>2.5</td> <td>3</td> 210 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 211 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 212 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 213 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 214 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 215 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 216 *<tr style="text-align:right"><td>-2.5</td> <td>-3</td> 217 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 218 *</table> 219 */ 220 HALF_UP(BigDecimal.ROUND_HALF_UP), 221 222 /** 223 * Rounding mode to round towards {@literal "nearest neighbor"} 224 * unless both neighbors are equidistant, in which case round 225 * down. Behaves as for {@code RoundingMode.UP} if the discarded 226 * fraction is > 0.5; otherwise, behaves as for 227 * {@code RoundingMode.DOWN}. 228 * 229 *<p>Example: 230 *<table border> 231 * <caption><b>Rounding mode HALF_DOWN Examples</b></caption> 232 *<tr style="vertical-align:top"><th>Input Number</th> 233 * <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding 234 *<tr style="text-align:right"><td>5.5</td> <td>5</td> 235 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 236 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 237 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 238 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 239 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 240 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 241 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 242 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 243 *<tr style="text-align:right"><td>-5.5</td> <td>-5</td> 244 *</table> 245 */ 246 HALF_DOWN(BigDecimal.ROUND_HALF_DOWN), 247 248 /** 249 * Rounding mode to round towards the {@literal "nearest neighbor"} 250 * unless both neighbors are equidistant, in which case, round 251 * towards the even neighbor. Behaves as for 252 * {@code RoundingMode.HALF_UP} if the digit to the left of the 253 * discarded fraction is odd; behaves as for 254 * {@code RoundingMode.HALF_DOWN} if it's even. Note that this 255 * is the rounding mode that statistically minimizes cumulative 256 * error when applied repeatedly over a sequence of calculations. 257 * It is sometimes known as {@literal "Banker's rounding,"} and is 258 * chiefly used in the USA. This rounding mode is analogous to 259 * the rounding policy used for {@code float} and {@code double} 260 * arithmetic in Java. 261 * 262 *<p>Example: 263 *<table border> 264 * <caption><b>Rounding mode HALF_EVEN Examples</b></caption> 265 *<tr style="vertical-align:top"><th>Input Number</th> 266 * <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding 267 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 268 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 269 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 270 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 271 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 272 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 273 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 274 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 275 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 276 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 277 *</table> 278 */ 279 HALF_EVEN(BigDecimal.ROUND_HALF_EVEN), 280 281 /** 282 * Rounding mode to assert that the requested operation has an exact 283 * result, hence no rounding is necessary. If this rounding mode is 284 * specified on an operation that yields an inexact result, an 285 * {@code ArithmeticException} is thrown. 286 *<p>Example: 287 *<table border> 288 * <caption><b>Rounding mode UNNECESSARY Examples</b></caption> 289 *<tr style="vertical-align:top"><th>Input Number</th> 290 * <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding 291 *<tr style="text-align:right"><td>5.5</td> <td>throw {@code ArithmeticException}</td> 292 *<tr style="text-align:right"><td>2.5</td> <td>throw {@code ArithmeticException}</td> 293 *<tr style="text-align:right"><td>1.6</td> <td>throw {@code ArithmeticException}</td> 294 *<tr style="text-align:right"><td>1.1</td> <td>throw {@code ArithmeticException}</td> 295 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 296 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 297 *<tr style="text-align:right"><td>-1.1</td> <td>throw {@code ArithmeticException}</td> 298 *<tr style="text-align:right"><td>-1.6</td> <td>throw {@code ArithmeticException}</td> 299 *<tr style="text-align:right"><td>-2.5</td> <td>throw {@code ArithmeticException}</td> 300 *<tr style="text-align:right"><td>-5.5</td> <td>throw {@code ArithmeticException}</td> 301 *</table> 302 */ 303 UNNECESSARY(BigDecimal.ROUND_UNNECESSARY); 304 305 // Corresponding BigDecimal rounding constant 306 final int oldMode; 307 308 /** 309 * Constructor 310 * 311 * @param oldMode The {@code BigDecimal} constant corresponding to 312 * this mode 313 */ 314 private RoundingMode(int oldMode) { 315 this.oldMode = oldMode; 316 } 317 318 /** 319 * Returns the {@code RoundingMode} object corresponding to a 320 * legacy integer rounding mode constant in {@link BigDecimal}. | 34 * the least significant returned digit of a rounded result is to be 35 * calculated. If fewer digits are returned than the digits needed to 36 * represent the exact numerical result, the discarded digits will be 37 * referred to as the <i>discarded fraction</i> regardless the digits' 38 * contribution to the value of the number. In other words, 39 * considered as a numerical value, the discarded fraction could have 40 * an absolute value greater than one. 41 * 42 * <p>Each rounding mode description includes a table listing how 43 * different two-digit decimal values would round to a one digit 44 * decimal value under the rounding mode in question. The result 45 * column in the tables could be gotten by creating a 46 * {@code BigDecimal} number with the specified value, forming a 47 * {@link MathContext} object with the proper settings 48 * ({@code precision} set to {@code 1}, and the 49 * {@code roundingMode} set to the rounding mode in question), and 50 * calling {@link BigDecimal#round round} on this number with the 51 * proper {@code MathContext}. A summary table showing the results 52 * of these rounding operations for all rounding modes appears below. 53 * 54 *<table class="plain"> 55 * <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption> 56 * <thead> 57 * <tr><th></th><th colspan=8>Result of rounding input to one digit with the given 58 * rounding mode</th> 59 * <tr style="vertical-align:top"> 60 * <th>Input Number</th> <th>{@code UP}</th> 61 * <th>{@code DOWN}</th> 62 * <th>{@code CEILING}</th> 63 * <th>{@code FLOOR}</th> 64 * <th>{@code HALF_UP}</th> 65 * <th>{@code HALF_DOWN}</th> 66 * <th>{@code HALF_EVEN}</th> 67 * <th>{@code UNNECESSARY}</th> 68 * </thead> 69 * <tbody> 70 * 71 * <tr style="text-align:right"><td>5.5</td> <td>6</td> <td>5</td> <td>6</td> <td>5</td> <td>6</td> <td>5</td> <td>6</td> <td>throw {@code ArithmeticException}</td> 72 * <tr style="text-align:right"><td>2.5</td> <td>3</td> <td>2</td> <td>3</td> <td>2</td> <td>3</td> <td>2</td> <td>2</td> <td>throw {@code ArithmeticException}</td> 73 * <tr style="text-align:right"><td>1.6</td> <td>2</td> <td>1</td> <td>2</td> <td>1</td> <td>2</td> <td>2</td> <td>2</td> <td>throw {@code ArithmeticException}</td> 74 * <tr style="text-align:right"><td>1.1</td> <td>2</td> <td>1</td> <td>2</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>throw {@code ArithmeticException}</td> 75 * <tr style="text-align:right"><td>1.0</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> 76 * <tr style="text-align:right"><td>-1.0</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> 77 * <tr style="text-align:right"><td>-1.1</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>throw {@code ArithmeticException}</td> 78 * <tr style="text-align:right"><td>-1.6</td> <td>-2</td> <td>-1</td> <td>-1</td> <td>-2</td> <td>-2</td> <td>-2</td> <td>-2</td> <td>throw {@code ArithmeticException}</td> 79 * <tr style="text-align:right"><td>-2.5</td> <td>-3</td> <td>-2</td> <td>-2</td> <td>-3</td> <td>-3</td> <td>-2</td> <td>-2</td> <td>throw {@code ArithmeticException}</td> 80 * <tr style="text-align:right"><td>-5.5</td> <td>-6</td> <td>-5</td> <td>-5</td> <td>-6</td> <td>-6</td> <td>-5</td> <td>-6</td> <td>throw {@code ArithmeticException}</td> 81 * </tbody> 82 * </table> 83 * 84 * 85 * <p>This {@code enum} is intended to replace the integer-based 86 * enumeration of rounding mode constants in {@link BigDecimal} 87 * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN}, 88 * etc. ). 89 * 90 * @see BigDecimal 91 * @see MathContext 92 * @author Josh Bloch 93 * @author Mike Cowlishaw 94 * @author Joseph D. Darcy 95 * @since 1.5 96 */ 97 @SuppressWarnings("deprecation") // Legacy rounding mode constants in BigDecimal 98 public enum RoundingMode { 99 100 /** 101 * Rounding mode to round away from zero. Always increments the 102 * digit prior to a non-zero discarded fraction. Note that this 103 * rounding mode never decreases the magnitude of the calculated 104 * value. 105 * 106 *<p>Example: 107 *<table class="plain"> 108 * <caption><b>Rounding mode UP Examples</b></caption> 109 *<thead> 110 *<tr style="vertical-align:top"><th>Input Number</th> 111 * <th>Input rounded to one digit<br> with {@code UP} rounding 112 *</thead> 113 *<tbody> 114 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 115 *<tr style="text-align:right"><td>2.5</td> <td>3</td> 116 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 117 *<tr style="text-align:right"><td>1.1</td> <td>2</td> 118 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 119 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 120 *<tr style="text-align:right"><td>-1.1</td> <td>-2</td> 121 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 122 *<tr style="text-align:right"><td>-2.5</td> <td>-3</td> 123 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 124 *</tbody> 125 *</table> 126 */ 127 UP(BigDecimal.ROUND_UP), 128 129 /** 130 * Rounding mode to round towards zero. Never increments the digit 131 * prior to a discarded fraction (i.e., truncates). Note that this 132 * rounding mode never increases the magnitude of the calculated value. 133 * 134 *<p>Example: 135 *<table class="plain"> 136 * <caption><b>Rounding mode DOWN Examples</b></caption> 137 *<thead> 138 *<tr style="vertical-align:top"><th>Input Number</th> 139 * <th>Input rounded to one digit<br> with {@code DOWN} rounding 140 *</thead> 141 *<tbody> 142 *<tr style="text-align:right"><td>5.5</td> <td>5</td> 143 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 144 *<tr style="text-align:right"><td>1.6</td> <td>1</td> 145 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 146 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 147 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 148 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 149 *<tr style="text-align:right"><td>-1.6</td> <td>-1</td> 150 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 151 *<tr style="text-align:right"><td>-5.5</td> <td>-5</td> 152 *</tbody> 153 *</table> 154 */ 155 DOWN(BigDecimal.ROUND_DOWN), 156 157 /** 158 * Rounding mode to round towards positive infinity. If the 159 * result is positive, behaves as for {@code RoundingMode.UP}; 160 * if negative, behaves as for {@code RoundingMode.DOWN}. Note 161 * that this rounding mode never decreases the calculated value. 162 * 163 *<p>Example: 164 *<table class="plain"> 165 * <caption><b>Rounding mode CEILING Examples</b></caption> 166 *<thead> 167 *<tr style="vertical-align:top"><th>Input Number</th> 168 * <th>Input rounded to one digit<br> with {@code CEILING} rounding 169 *</thead> 170 *<tbody> 171 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 172 *<tr style="text-align:right"><td>2.5</td> <td>3</td> 173 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 174 *<tr style="text-align:right"><td>1.1</td> <td>2</td> 175 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 176 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 177 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 178 *<tr style="text-align:right"><td>-1.6</td> <td>-1</td> 179 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 180 *<tr style="text-align:right"><td>-5.5</td> <td>-5</td> 181 *</tbody> 182 *</table> 183 */ 184 CEILING(BigDecimal.ROUND_CEILING), 185 186 /** 187 * Rounding mode to round towards negative infinity. If the 188 * result is positive, behave as for {@code RoundingMode.DOWN}; 189 * if negative, behave as for {@code RoundingMode.UP}. Note that 190 * this rounding mode never increases the calculated value. 191 * 192 *<p>Example: 193 *<table class="plain"> 194 * <caption><b>Rounding mode FLOOR Examples</b></caption> 195 *<thead> 196 *<tr style="vertical-align:top"><th>Input Number</th> 197 * <th>Input rounded to one digit<br> with {@code FLOOR} rounding 198 *</thead> 199 *<tbody> 200 *<tr style="text-align:right"><td>5.5</td> <td>5</td> 201 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 202 *<tr style="text-align:right"><td>1.6</td> <td>1</td> 203 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 204 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 205 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 206 *<tr style="text-align:right"><td>-1.1</td> <td>-2</td> 207 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 208 *<tr style="text-align:right"><td>-2.5</td> <td>-3</td> 209 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 210 *</tbody> 211 *</table> 212 */ 213 FLOOR(BigDecimal.ROUND_FLOOR), 214 215 /** 216 * Rounding mode to round towards {@literal "nearest neighbor"} 217 * unless both neighbors are equidistant, in which case round up. 218 * Behaves as for {@code RoundingMode.UP} if the discarded 219 * fraction is ≥ 0.5; otherwise, behaves as for 220 * {@code RoundingMode.DOWN}. Note that this is the rounding 221 * mode commonly taught at school. 222 * 223 *<p>Example: 224 *<table class="plain"> 225 * <caption><b>Rounding mode HALF_UP Examples</b></caption> 226 *<thead> 227 *<tr style="vertical-align:top"><th>Input Number</th> 228 * <th>Input rounded to one digit<br> with {@code HALF_UP} rounding 229 *</thead> 230 *<tbody> 231 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 232 *<tr style="text-align:right"><td>2.5</td> <td>3</td> 233 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 234 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 235 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 236 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 237 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 238 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 239 *<tr style="text-align:right"><td>-2.5</td> <td>-3</td> 240 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 241 *</tbody> 242 *</table> 243 */ 244 HALF_UP(BigDecimal.ROUND_HALF_UP), 245 246 /** 247 * Rounding mode to round towards {@literal "nearest neighbor"} 248 * unless both neighbors are equidistant, in which case round 249 * down. Behaves as for {@code RoundingMode.UP} if the discarded 250 * fraction is > 0.5; otherwise, behaves as for 251 * {@code RoundingMode.DOWN}. 252 * 253 *<p>Example: 254 *<table class="plain"> 255 * <caption><b>Rounding mode HALF_DOWN Examples</b></caption> 256 *<thead> 257 *<tr style="vertical-align:top"><th>Input Number</th> 258 * <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding 259 *</thead> 260 *<tbody> 261 *<tr style="text-align:right"><td>5.5</td> <td>5</td> 262 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 263 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 264 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 265 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 266 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 267 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 268 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 269 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 270 *<tr style="text-align:right"><td>-5.5</td> <td>-5</td> 271 *</tbody> 272 *</table> 273 */ 274 HALF_DOWN(BigDecimal.ROUND_HALF_DOWN), 275 276 /** 277 * Rounding mode to round towards the {@literal "nearest neighbor"} 278 * unless both neighbors are equidistant, in which case, round 279 * towards the even neighbor. Behaves as for 280 * {@code RoundingMode.HALF_UP} if the digit to the left of the 281 * discarded fraction is odd; behaves as for 282 * {@code RoundingMode.HALF_DOWN} if it's even. Note that this 283 * is the rounding mode that statistically minimizes cumulative 284 * error when applied repeatedly over a sequence of calculations. 285 * It is sometimes known as {@literal "Banker's rounding,"} and is 286 * chiefly used in the USA. This rounding mode is analogous to 287 * the rounding policy used for {@code float} and {@code double} 288 * arithmetic in Java. 289 * 290 *<p>Example: 291 *<table class="plain"> 292 * <caption><b>Rounding mode HALF_EVEN Examples</b></caption> 293 *<thead> 294 *<tr style="vertical-align:top"><th>Input Number</th> 295 * <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding 296 *</thead> 297 *<tbody> 298 *<tr style="text-align:right"><td>5.5</td> <td>6</td> 299 *<tr style="text-align:right"><td>2.5</td> <td>2</td> 300 *<tr style="text-align:right"><td>1.6</td> <td>2</td> 301 *<tr style="text-align:right"><td>1.1</td> <td>1</td> 302 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 303 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 304 *<tr style="text-align:right"><td>-1.1</td> <td>-1</td> 305 *<tr style="text-align:right"><td>-1.6</td> <td>-2</td> 306 *<tr style="text-align:right"><td>-2.5</td> <td>-2</td> 307 *<tr style="text-align:right"><td>-5.5</td> <td>-6</td> 308 *</tbody> 309 *</table> 310 */ 311 HALF_EVEN(BigDecimal.ROUND_HALF_EVEN), 312 313 /** 314 * Rounding mode to assert that the requested operation has an exact 315 * result, hence no rounding is necessary. If this rounding mode is 316 * specified on an operation that yields an inexact result, an 317 * {@code ArithmeticException} is thrown. 318 *<p>Example: 319 *<table class="plain"> 320 * <caption><b>Rounding mode UNNECESSARY Examples</b></caption> 321 *<thead> 322 *<tr style="vertical-align:top"><th>Input Number</th> 323 * <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding 324 *</thead> 325 *<tbody> 326 *<tr style="text-align:right"><td>5.5</td> <td>throw {@code ArithmeticException}</td> 327 *<tr style="text-align:right"><td>2.5</td> <td>throw {@code ArithmeticException}</td> 328 *<tr style="text-align:right"><td>1.6</td> <td>throw {@code ArithmeticException}</td> 329 *<tr style="text-align:right"><td>1.1</td> <td>throw {@code ArithmeticException}</td> 330 *<tr style="text-align:right"><td>1.0</td> <td>1</td> 331 *<tr style="text-align:right"><td>-1.0</td> <td>-1</td> 332 *<tr style="text-align:right"><td>-1.1</td> <td>throw {@code ArithmeticException}</td> 333 *<tr style="text-align:right"><td>-1.6</td> <td>throw {@code ArithmeticException}</td> 334 *<tr style="text-align:right"><td>-2.5</td> <td>throw {@code ArithmeticException}</td> 335 *<tr style="text-align:right"><td>-5.5</td> <td>throw {@code ArithmeticException}</td> 336 *</tbody> 337 *</table> 338 */ 339 UNNECESSARY(BigDecimal.ROUND_UNNECESSARY); 340 341 // Corresponding BigDecimal rounding constant 342 final int oldMode; 343 344 /** 345 * Constructor 346 * 347 * @param oldMode The {@code BigDecimal} constant corresponding to 348 * this mode 349 */ 350 private RoundingMode(int oldMode) { 351 this.oldMode = oldMode; 352 } 353 354 /** 355 * Returns the {@code RoundingMode} object corresponding to a 356 * legacy integer rounding mode constant in {@link BigDecimal}. |