1 /*
   2  * Copyright (c) 2003, 2017, 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 behavior</i> for numerical operations
  33  * capable of discarding precision. Each rounding mode indicates how
  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="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  * @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="striped">
 108          * <caption>Rounding mode UP Examples</caption>
 109          *<thead>
 110          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 111          *    <th scope="col">Input rounded to one digit<br> with {@code UP} rounding
 112          *</thead>
 113          *<tbody style="text-align:right">
 114          *<tr><th scope="row">5.5</th>  <td>6</td>
 115          *<tr><th scope="row">2.5</th>  <td>3</td>
 116          *<tr><th scope="row">1.6</th>  <td>2</td>
 117          *<tr><th scope="row">1.1</th>  <td>2</td>
 118          *<tr><th scope="row">1.0</th>  <td>1</td>
 119          *<tr><th scope="row">-1.0</th> <td>-1</td>
 120          *<tr><th scope="row">-1.1</th> <td>-2</td>
 121          *<tr><th scope="row">-1.6</th> <td>-2</td>
 122          *<tr><th scope="row">-2.5</th> <td>-3</td>
 123          *<tr><th scope="row">-5.5</th> <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="striped">
 136          * <caption>Rounding mode DOWN Examples</caption>
 137          *<thead>
 138          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 139          *    <th scope="col">Input rounded to one digit<br> with {@code DOWN} rounding
 140          *</thead>
 141          *<tbody style="text-align:right">
 142          *<tr><th scope="row">5.5</th>  <td>5</td>
 143          *<tr><th scope="row">2.5</th>  <td>2</td>
 144          *<tr><th scope="row">1.6</th>  <td>1</td>
 145          *<tr><th scope="row">1.1</th>  <td>1</td>
 146          *<tr><th scope="row">1.0</th>  <td>1</td>
 147          *<tr><th scope="row">-1.0</th> <td>-1</td>
 148          *<tr><th scope="row">-1.1</th> <td>-1</td>
 149          *<tr><th scope="row">-1.6</th> <td>-1</td>
 150          *<tr><th scope="row">-2.5</th> <td>-2</td>
 151          *<tr><th scope="row">-5.5</th> <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="striped">
 165          * <caption>Rounding mode CEILING Examples</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 style="text-align:right">
 171          *<tr><th scope="row">5.5</th>  <td>6</td>
 172          *<tr><th scope="row">2.5</th>  <td>3</td>
 173          *<tr><th scope="row">1.6</th>  <td>2</td>
 174          *<tr><th scope="row">1.1</th>  <td>2</td>
 175          *<tr><th scope="row">1.0</th>  <td>1</td>
 176          *<tr><th scope="row">-1.0</th> <td>-1</td>
 177          *<tr><th scope="row">-1.1</th> <td>-1</td>
 178          *<tr><th scope="row">-1.6</th> <td>-1</td>
 179          *<tr><th scope="row">-2.5</th> <td>-2</td>
 180          *<tr><th scope="row">-5.5</th> <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="striped">
 194          * <caption>Rounding mode FLOOR Examples</caption>
 195          *<thead>
 196          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 197          *    <th scope="col">Input rounded to one digit<br> with {@code FLOOR} rounding
 198          *</thead>
 199          *<tbody style="text-align:right">
 200          *<tr><th scope="row">5.5</th>  <td>5</td>
 201          *<tr><th scope="row">2.5</th>  <td>2</td>
 202          *<tr><th scope="row">1.6</th>  <td>1</td>
 203          *<tr><th scope="row">1.1</th>  <td>1</td>
 204          *<tr><th scope="row">1.0</th>  <td>1</td>
 205          *<tr><th scope="row">-1.0</th> <td>-1</td>
 206          *<tr><th scope="row">-1.1</th> <td>-2</td>
 207          *<tr><th scope="row">-1.6</th> <td>-2</td>
 208          *<tr><th scope="row">-2.5</th> <td>-3</td>
 209          *<tr><th scope="row">-5.5</th> <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 &ge; 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="striped">
 225          * <caption>Rounding mode HALF_UP Examples</caption>
 226          *<thead>
 227          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 228          *    <th scope="col">Input rounded to one digit<br> with {@code HALF_UP} rounding
 229          *</thead>
 230          *<tbody style="text-align:right">
 231          *<tr><th scope="row">5.5</th>  <td>6</td>
 232          *<tr><th scope="row">2.5</th>  <td>3</td>
 233          *<tr><th scope="row">1.6</th>  <td>2</td>
 234          *<tr><th scope="row">1.1</th>  <td>1</td>
 235          *<tr><th scope="row">1.0</th>  <td>1</td>
 236          *<tr><th scope="row">-1.0</th> <td>-1</td>
 237          *<tr><th scope="row">-1.1</th> <td>-1</td>
 238          *<tr><th scope="row">-1.6</th> <td>-2</td>
 239          *<tr><th scope="row">-2.5</th> <td>-3</td>
 240          *<tr><th scope="row">-5.5</th> <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 &gt; 0.5; otherwise, behaves as for
 251          * {@code RoundingMode.DOWN}.
 252          *
 253          *<p>Example:
 254          *<table class="striped">
 255          * <caption>Rounding mode HALF_DOWN Examples</caption>
 256          *<thead>
 257          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 258          *    <th scope="col">Input rounded to one digit<br> with {@code HALF_DOWN} rounding
 259          *</thead>
 260          *<tbody style="text-align:right">
 261          *<tr><th scope="row">5.5</th>  <td>5</td>
 262          *<tr><th scope="row">2.5</th>  <td>2</td>
 263          *<tr><th scope="row">1.6</th>  <td>2</td>
 264          *<tr><th scope="row">1.1</th>  <td>1</td>
 265          *<tr><th scope="row">1.0</th>  <td>1</td>
 266          *<tr><th scope="row">-1.0</th> <td>-1</td>
 267          *<tr><th scope="row">-1.1</th> <td>-1</td>
 268          *<tr><th scope="row">-1.6</th> <td>-2</td>
 269          *<tr><th scope="row">-2.5</th> <td>-2</td>
 270          *<tr><th scope="row">-5.5</th> <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="striped">
 292          * <caption>Rounding mode HALF_EVEN Examples</caption>
 293          *<thead>
 294          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 295          *    <th scope="col">Input rounded to one digit<br> with {@code HALF_EVEN} rounding
 296          *</thead>
 297          *<tbody style="text-align:right">
 298          *<tr><th scope="row">5.5</th>  <td>6</td>
 299          *<tr><th scope="row">2.5</th>  <td>2</td>
 300          *<tr><th scope="row">1.6</th>  <td>2</td>
 301          *<tr><th scope="row">1.1</th>  <td>1</td>
 302          *<tr><th scope="row">1.0</th>  <td>1</td>
 303          *<tr><th scope="row">-1.0</th> <td>-1</td>
 304          *<tr><th scope="row">-1.1</th> <td>-1</td>
 305          *<tr><th scope="row">-1.6</th> <td>-2</td>
 306          *<tr><th scope="row">-2.5</th> <td>-2</td>
 307          *<tr><th scope="row">-5.5</th> <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="striped">
 320          * <caption>Rounding mode UNNECESSARY Examples</caption>
 321          *<thead>
 322          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 323          *    <th scope="col">Input rounded to one digit<br> with {@code UNNECESSARY} rounding
 324          *</thead>
 325          *<tbody style="text-align:right">
 326          *<tr><th scope="row">5.5</th>  <td>throw {@code ArithmeticException}</td>
 327          *<tr><th scope="row">2.5</th>  <td>throw {@code ArithmeticException}</td>
 328          *<tr><th scope="row">1.6</th>  <td>throw {@code ArithmeticException}</td>
 329          *<tr><th scope="row">1.1</th>  <td>throw {@code ArithmeticException}</td>
 330          *<tr><th scope="row">1.0</th>  <td>1</td>
 331          *<tr><th scope="row">-1.0</th> <td>-1</td>
 332          *<tr><th scope="row">-1.1</th> <td>throw {@code ArithmeticException}</td>
 333          *<tr><th scope="row">-1.6</th> <td>throw {@code ArithmeticException}</td>
 334          *<tr><th scope="row">-2.5</th> <td>throw {@code ArithmeticException}</td>
 335          *<tr><th scope="row">-5.5</th> <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}.
 357      *
 358      * @param  rm legacy integer rounding mode to convert
 359      * @return {@code RoundingMode} corresponding to the given integer.
 360      * @throws IllegalArgumentException integer is out of range
 361      */
 362     public static RoundingMode valueOf(int rm) {
 363         switch(rm) {
 364 
 365         case BigDecimal.ROUND_UP:
 366             return UP;
 367 
 368         case BigDecimal.ROUND_DOWN:
 369             return DOWN;
 370 
 371         case BigDecimal.ROUND_CEILING:
 372             return CEILING;
 373 
 374         case BigDecimal.ROUND_FLOOR:
 375             return FLOOR;
 376 
 377         case BigDecimal.ROUND_HALF_UP:
 378             return HALF_UP;
 379 
 380         case BigDecimal.ROUND_HALF_DOWN:
 381             return HALF_DOWN;
 382 
 383         case BigDecimal.ROUND_HALF_EVEN:
 384             return HALF_EVEN;
 385 
 386         case BigDecimal.ROUND_UNNECESSARY:
 387             return UNNECESSARY;
 388 
 389         default:
 390             throw new IllegalArgumentException("argument out of range");
 391         }
 392     }
 393 }