1 /* 2 * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Portions Copyright IBM Corporation, 2001. All Rights Reserved. 28 */ 29 package java.math; 30 31 /** 32 * Specifies a <i>rounding policy</i> for numerical operations capable 33 * of discarding precision. Each rounding mode indicates how the least 34 * significant returned digit of a rounded result is to be calculated. 35 * If fewer digits are returned than the digits needed to represent 36 * the exact numerical result, the discarded digits will be referred 37 * 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="striped"> 55 * <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption> 56 * <thead> 57 * <tr><th scope="col" rowspan="2">Input Number</th><th scope="col"colspan=8>Result of rounding input to one digit with the given 58 * rounding mode</th> 59 * <tr style="vertical-align:top"> 60 * <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 style="text-align:right"> 70 * 71 * <tr><th scope="row">5.5</th> <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><th scope="row">2.5</th> <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><th scope="row">1.6</th> <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><th scope="row">1.1</th> <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><th scope="row">1.0</th> <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><th scope="row">-1.0</th> <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><th scope="row">-1.1</th> <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><th scope="row">-1.6</th> <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><th scope="row">-2.5</th> <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><th scope="row">-5.5</th> <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 * @apiNote 91 * Five of the rounding modes declared in this class correspond to 92 * rounding-direction attributes defined in the <cite>IEEE Standard 93 * for Floating-Point Arithmetic</cite>, IEEE 754-2019. Where present, 94 * this correspondence will be noted in the documentation of the 95 * particular constant. 96 * 97 * @see BigDecimal 98 * @see MathContext 99 * @author Josh Bloch 100 * @author Mike Cowlishaw 101 * @author Joseph D. Darcy 102 * @since 1.5 103 */ 104 @SuppressWarnings("deprecation") // Legacy rounding mode constants in BigDecimal 105 public enum RoundingMode { 106 107 /** 108 * Rounding mode to round away from zero. Always increments the 109 * digit prior to a non-zero discarded fraction. Note that this 110 * rounding mode never decreases the magnitude of the calculated 111 * value. 112 * 113 *<p>Example: 114 *<table class="striped"> 115 * <caption>Rounding mode UP Examples</caption> 116 *<thead> 117 *<tr style="vertical-align:top"><th scope="col">Input Number</th> 118 * <th scope="col">Input rounded to one digit<br> with {@code UP} rounding 119 *</thead> 120 *<tbody style="text-align:right"> 121 *<tr><th scope="row">5.5</th> <td>6</td> 122 *<tr><th scope="row">2.5</th> <td>3</td> 123 *<tr><th scope="row">1.6</th> <td>2</td> 124 *<tr><th scope="row">1.1</th> <td>2</td> 125 *<tr><th scope="row">1.0</th> <td>1</td> 126 *<tr><th scope="row">-1.0</th> <td>-1</td> 127 *<tr><th scope="row">-1.1</th> <td>-2</td> 128 *<tr><th scope="row">-1.6</th> <td>-2</td> 129 *<tr><th scope="row">-2.5</th> <td>-3</td> 130 *<tr><th scope="row">-5.5</th> <td>-6</td> 131 *</tbody> 132 *</table> 133 */ 134 UP(BigDecimal.ROUND_UP), 135 136 /** 137 * Rounding mode to round towards zero. Never increments the digit 138 * prior to a discarded fraction (i.e., truncates). Note that this 139 * rounding mode never increases the magnitude of the calculated value. 140 * This mode corresponds to the IEEE 754-2019 rounding-direction 141 * attribute roundTowardZero. 142 * 143 *<p>Example: 144 *<table class="striped"> 145 * <caption>Rounding mode DOWN Examples</caption> 146 *<thead> 147 *<tr style="vertical-align:top"><th scope="col">Input Number</th> 148 * <th scope="col">Input rounded to one digit<br> with {@code DOWN} rounding 149 *</thead> 150 *<tbody style="text-align:right"> 151 *<tr><th scope="row">5.5</th> <td>5</td> 152 *<tr><th scope="row">2.5</th> <td>2</td> 153 *<tr><th scope="row">1.6</th> <td>1</td> 154 *<tr><th scope="row">1.1</th> <td>1</td> 155 *<tr><th scope="row">1.0</th> <td>1</td> 156 *<tr><th scope="row">-1.0</th> <td>-1</td> 157 *<tr><th scope="row">-1.1</th> <td>-1</td> 158 *<tr><th scope="row">-1.6</th> <td>-1</td> 159 *<tr><th scope="row">-2.5</th> <td>-2</td> 160 *<tr><th scope="row">-5.5</th> <td>-5</td> 161 *</tbody> 162 *</table> 163 */ 164 DOWN(BigDecimal.ROUND_DOWN), 165 166 /** 167 * Rounding mode to round towards positive infinity. If the 168 * result is positive, behaves as for {@code RoundingMode.UP}; 169 * if negative, behaves as for {@code RoundingMode.DOWN}. Note 170 * that this rounding mode never decreases the calculated value. 171 * This mode corresponds to the IEEE 754-2019 rounding-direction 172 * attribute roundTowardPositive. 173 * 174 *<p>Example: 175 *<table class="striped"> 176 * <caption>Rounding mode CEILING Examples</caption> 177 *<thead> 178 *<tr style="vertical-align:top"><th>Input Number</th> 179 * <th>Input rounded to one digit<br> with {@code CEILING} rounding 180 *</thead> 181 *<tbody style="text-align:right"> 182 *<tr><th scope="row">5.5</th> <td>6</td> 183 *<tr><th scope="row">2.5</th> <td>3</td> 184 *<tr><th scope="row">1.6</th> <td>2</td> 185 *<tr><th scope="row">1.1</th> <td>2</td> 186 *<tr><th scope="row">1.0</th> <td>1</td> 187 *<tr><th scope="row">-1.0</th> <td>-1</td> 188 *<tr><th scope="row">-1.1</th> <td>-1</td> 189 *<tr><th scope="row">-1.6</th> <td>-1</td> 190 *<tr><th scope="row">-2.5</th> <td>-2</td> 191 *<tr><th scope="row">-5.5</th> <td>-5</td> 192 *</tbody> 193 *</table> 194 */ 195 CEILING(BigDecimal.ROUND_CEILING), 196 197 /** 198 * Rounding mode to round towards negative infinity. If the 199 * result is positive, behave as for {@code RoundingMode.DOWN}; 200 * if negative, behave as for {@code RoundingMode.UP}. Note that 201 * this rounding mode never increases the calculated value. 202 * This mode corresponds to the IEEE 754-2019 rounding-direction 203 * attribute roundTowardNegative. 204 * 205 *<p>Example: 206 *<table class="striped"> 207 * <caption>Rounding mode FLOOR Examples</caption> 208 *<thead> 209 *<tr style="vertical-align:top"><th scope="col">Input Number</th> 210 * <th scope="col">Input rounded to one digit<br> with {@code FLOOR} rounding 211 *</thead> 212 *<tbody style="text-align:right"> 213 *<tr><th scope="row">5.5</th> <td>5</td> 214 *<tr><th scope="row">2.5</th> <td>2</td> 215 *<tr><th scope="row">1.6</th> <td>1</td> 216 *<tr><th scope="row">1.1</th> <td>1</td> 217 *<tr><th scope="row">1.0</th> <td>1</td> 218 *<tr><th scope="row">-1.0</th> <td>-1</td> 219 *<tr><th scope="row">-1.1</th> <td>-2</td> 220 *<tr><th scope="row">-1.6</th> <td>-2</td> 221 *<tr><th scope="row">-2.5</th> <td>-3</td> 222 *<tr><th scope="row">-5.5</th> <td>-6</td> 223 *</tbody> 224 *</table> 225 */ 226 FLOOR(BigDecimal.ROUND_FLOOR), 227 228 /** 229 * Rounding mode to round towards {@literal "nearest neighbor"} 230 * unless both neighbors are equidistant, in which case round up. 231 * Behaves as for {@code RoundingMode.UP} if the discarded 232 * fraction is ≥ 0.5; otherwise, behaves as for 233 * {@code RoundingMode.DOWN}. Note that this is the rounding 234 * mode commonly taught at school. 235 * This mode corresponds to the IEEE 754-2019 rounding-direction 236 * attribute roundTiesToAway. 237 * 238 *<p>Example: 239 *<table class="striped"> 240 * <caption>Rounding mode HALF_UP Examples</caption> 241 *<thead> 242 *<tr style="vertical-align:top"><th scope="col">Input Number</th> 243 * <th scope="col">Input rounded to one digit<br> with {@code HALF_UP} rounding 244 *</thead> 245 *<tbody style="text-align:right"> 246 *<tr><th scope="row">5.5</th> <td>6</td> 247 *<tr><th scope="row">2.5</th> <td>3</td> 248 *<tr><th scope="row">1.6</th> <td>2</td> 249 *<tr><th scope="row">1.1</th> <td>1</td> 250 *<tr><th scope="row">1.0</th> <td>1</td> 251 *<tr><th scope="row">-1.0</th> <td>-1</td> 252 *<tr><th scope="row">-1.1</th> <td>-1</td> 253 *<tr><th scope="row">-1.6</th> <td>-2</td> 254 *<tr><th scope="row">-2.5</th> <td>-3</td> 255 *<tr><th scope="row">-5.5</th> <td>-6</td> 256 *</tbody> 257 *</table> 258 */ 259 HALF_UP(BigDecimal.ROUND_HALF_UP), 260 261 /** 262 * Rounding mode to round towards {@literal "nearest neighbor"} 263 * unless both neighbors are equidistant, in which case round 264 * down. Behaves as for {@code RoundingMode.UP} if the discarded 265 * fraction is > 0.5; otherwise, behaves as for 266 * {@code RoundingMode.DOWN}. 267 * 268 *<p>Example: 269 *<table class="striped"> 270 * <caption>Rounding mode HALF_DOWN Examples</caption> 271 *<thead> 272 *<tr style="vertical-align:top"><th scope="col">Input Number</th> 273 * <th scope="col">Input rounded to one digit<br> with {@code HALF_DOWN} rounding 274 *</thead> 275 *<tbody style="text-align:right"> 276 *<tr><th scope="row">5.5</th> <td>5</td> 277 *<tr><th scope="row">2.5</th> <td>2</td> 278 *<tr><th scope="row">1.6</th> <td>2</td> 279 *<tr><th scope="row">1.1</th> <td>1</td> 280 *<tr><th scope="row">1.0</th> <td>1</td> 281 *<tr><th scope="row">-1.0</th> <td>-1</td> 282 *<tr><th scope="row">-1.1</th> <td>-1</td> 283 *<tr><th scope="row">-1.6</th> <td>-2</td> 284 *<tr><th scope="row">-2.5</th> <td>-2</td> 285 *<tr><th scope="row">-5.5</th> <td>-5</td> 286 *</tbody> 287 *</table> 288 */ 289 HALF_DOWN(BigDecimal.ROUND_HALF_DOWN), 290 291 /** 292 * Rounding mode to round towards the {@literal "nearest neighbor"} 293 * unless both neighbors are equidistant, in which case, round 294 * towards the even neighbor. Behaves as for 295 * {@code RoundingMode.HALF_UP} if the digit to the left of the 296 * discarded fraction is odd; behaves as for 297 * {@code RoundingMode.HALF_DOWN} if it's even. Note that this 298 * is the rounding mode that statistically minimizes cumulative 299 * error when applied repeatedly over a sequence of calculations. 300 * It is sometimes known as {@literal "Banker's rounding,"} and is 301 * chiefly used in the USA. This rounding mode is analogous to 302 * the rounding policy used for {@code float} and {@code double} 303 * arithmetic in Java. 304 * This mode corresponds to the IEEE 754-2019 rounding-direction 305 * attribute roundTiesToEven. 306 * 307 *<p>Example: 308 *<table class="striped"> 309 * <caption>Rounding mode HALF_EVEN Examples</caption> 310 *<thead> 311 *<tr style="vertical-align:top"><th scope="col">Input Number</th> 312 * <th scope="col">Input rounded to one digit<br> with {@code HALF_EVEN} rounding 313 *</thead> 314 *<tbody style="text-align:right"> 315 *<tr><th scope="row">5.5</th> <td>6</td> 316 *<tr><th scope="row">2.5</th> <td>2</td> 317 *<tr><th scope="row">1.6</th> <td>2</td> 318 *<tr><th scope="row">1.1</th> <td>1</td> 319 *<tr><th scope="row">1.0</th> <td>1</td> 320 *<tr><th scope="row">-1.0</th> <td>-1</td> 321 *<tr><th scope="row">-1.1</th> <td>-1</td> 322 *<tr><th scope="row">-1.6</th> <td>-2</td> 323 *<tr><th scope="row">-2.5</th> <td>-2</td> 324 *<tr><th scope="row">-5.5</th> <td>-6</td> 325 *</tbody> 326 *</table> 327 */ 328 HALF_EVEN(BigDecimal.ROUND_HALF_EVEN), 329 330 /** 331 * Rounding mode to assert that the requested operation has an exact 332 * result, hence no rounding is necessary. If this rounding mode is 333 * specified on an operation that yields an inexact result, an 334 * {@code ArithmeticException} is thrown. 335 *<p>Example: 336 *<table class="striped"> 337 * <caption>Rounding mode UNNECESSARY Examples</caption> 338 *<thead> 339 *<tr style="vertical-align:top"><th scope="col">Input Number</th> 340 * <th scope="col">Input rounded to one digit<br> with {@code UNNECESSARY} rounding 341 *</thead> 342 *<tbody style="text-align:right"> 343 *<tr><th scope="row">5.5</th> <td>throw {@code ArithmeticException}</td> 344 *<tr><th scope="row">2.5</th> <td>throw {@code ArithmeticException}</td> 345 *<tr><th scope="row">1.6</th> <td>throw {@code ArithmeticException}</td> 346 *<tr><th scope="row">1.1</th> <td>throw {@code ArithmeticException}</td> 347 *<tr><th scope="row">1.0</th> <td>1</td> 348 *<tr><th scope="row">-1.0</th> <td>-1</td> 349 *<tr><th scope="row">-1.1</th> <td>throw {@code ArithmeticException}</td> 350 *<tr><th scope="row">-1.6</th> <td>throw {@code ArithmeticException}</td> 351 *<tr><th scope="row">-2.5</th> <td>throw {@code ArithmeticException}</td> 352 *<tr><th scope="row">-5.5</th> <td>throw {@code ArithmeticException}</td> 353 *</tbody> 354 *</table> 355 */ 356 UNNECESSARY(BigDecimal.ROUND_UNNECESSARY); 357 358 // Corresponding BigDecimal rounding constant 359 final int oldMode; 360 361 /** 362 * Constructor 363 * 364 * @param oldMode The {@code BigDecimal} constant corresponding to 365 * this mode 366 */ RoundingMode(int oldMode)367 private RoundingMode(int oldMode) { 368 this.oldMode = oldMode; 369 } 370 371 /** 372 * Returns the {@code RoundingMode} object corresponding to a 373 * legacy integer rounding mode constant in {@link BigDecimal}. 374 * 375 * @param rm legacy integer rounding mode to convert 376 * @return {@code RoundingMode} corresponding to the given integer. 377 * @throws IllegalArgumentException integer is out of range 378 */ valueOf(int rm)379 public static RoundingMode valueOf(int rm) { 380 return switch (rm) { 381 case BigDecimal.ROUND_UP -> UP; 382 case BigDecimal.ROUND_DOWN -> DOWN; 383 case BigDecimal.ROUND_CEILING -> CEILING; 384 case BigDecimal.ROUND_FLOOR -> FLOOR; 385 case BigDecimal.ROUND_HALF_UP -> HALF_UP; 386 case BigDecimal.ROUND_HALF_DOWN -> HALF_DOWN; 387 case BigDecimal.ROUND_HALF_EVEN -> HALF_EVEN; 388 case BigDecimal.ROUND_UNNECESSARY -> UNNECESSARY; 389 default -> throw new IllegalArgumentException("argument out of range"); 390 }; 391 } 392 } 393