1 /*
   2  * Copyright (c) 2003, 2011, 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  *<p>
  55  *<table border>
  56  * <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption>
  57  * <tr><th></th><th colspan=8>Result of rounding input to one digit with the given
  58  *                           rounding mode</th>
  59  * <tr valign=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  *
  69  * <tr 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>
  70  * <tr 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>
  71  * <tr 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>
  72  * <tr 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>
  73  * <tr 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 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>
  75  * <tr 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>
  76  * <tr 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>
  77  * <tr 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>
  78  * <tr 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>
  79  *</table>
  80  *
  81  *
  82  * <p>This {@code enum} is intended to replace the integer-based
  83  * enumeration of rounding mode constants in {@link BigDecimal}
  84  * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},
  85  * etc. ).
  86  *
  87  * @see     BigDecimal
  88  * @see     MathContext
  89  * @author  Josh Bloch
  90  * @author  Mike Cowlishaw
  91  * @author  Joseph D. Darcy
  92  * @since 1.5
  93  */
  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          *<tr valign=top><th>Input Number</th>
 105          *    <th>Input rounded to one digit<br> with {@code UP} rounding
 106          *<tr align=right><td>5.5</td>  <td>6</td>
 107          *<tr align=right><td>2.5</td>  <td>3</td>
 108          *<tr align=right><td>1.6</td>  <td>2</td>
 109          *<tr align=right><td>1.1</td>  <td>2</td>
 110          *<tr align=right><td>1.0</td>  <td>1</td>
 111          *<tr align=right><td>-1.0</td> <td>-1</td>
 112          *<tr align=right><td>-1.1</td> <td>-2</td>
 113          *<tr align=right><td>-1.6</td> <td>-2</td>
 114          *<tr align=right><td>-2.5</td> <td>-3</td>
 115          *<tr align=right><td>-5.5</td> <td>-6</td>
 116          *</table>
 117          */
 118     UP(BigDecimal.ROUND_UP),
 119 
 120         /**
 121          * Rounding mode to round towards zero.  Never increments the digit
 122          * prior to a discarded fraction (i.e., truncates).  Note that this
 123          * rounding mode never increases the magnitude of the calculated value.
 124          *
 125          *<p>Example:
 126          *<table border>
 127          *<tr valign=top><th>Input Number</th>
 128          *    <th>Input rounded to one digit<br> with {@code DOWN} rounding
 129          *<tr align=right><td>5.5</td>  <td>5</td>
 130          *<tr align=right><td>2.5</td>  <td>2</td>
 131          *<tr align=right><td>1.6</td>  <td>1</td>
 132          *<tr align=right><td>1.1</td>  <td>1</td>
 133          *<tr align=right><td>1.0</td>  <td>1</td>
 134          *<tr align=right><td>-1.0</td> <td>-1</td>
 135          *<tr align=right><td>-1.1</td> <td>-1</td>
 136          *<tr align=right><td>-1.6</td> <td>-1</td>
 137          *<tr align=right><td>-2.5</td> <td>-2</td>
 138          *<tr align=right><td>-5.5</td> <td>-5</td>
 139          *</table>
 140          */
 141     DOWN(BigDecimal.ROUND_DOWN),
 142 
 143         /**
 144          * Rounding mode to round towards positive infinity.  If the
 145          * result is positive, behaves as for {@code RoundingMode.UP};
 146          * if negative, behaves as for {@code RoundingMode.DOWN}.  Note
 147          * that this rounding mode never decreases the calculated value.
 148          *
 149          *<p>Example:
 150          *<table border>
 151          *<tr valign=top><th>Input Number</th>
 152          *    <th>Input rounded to one digit<br> with {@code CEILING} rounding
 153          *<tr align=right><td>5.5</td>  <td>6</td>
 154          *<tr align=right><td>2.5</td>  <td>3</td>
 155          *<tr align=right><td>1.6</td>  <td>2</td>
 156          *<tr align=right><td>1.1</td>  <td>2</td>
 157          *<tr align=right><td>1.0</td>  <td>1</td>
 158          *<tr align=right><td>-1.0</td> <td>-1</td>
 159          *<tr align=right><td>-1.1</td> <td>-1</td>
 160          *<tr align=right><td>-1.6</td> <td>-1</td>
 161          *<tr align=right><td>-2.5</td> <td>-2</td>
 162          *<tr align=right><td>-5.5</td> <td>-5</td>
 163          *</table>
 164          */
 165     CEILING(BigDecimal.ROUND_CEILING),
 166 
 167         /**
 168          * Rounding mode to round towards negative infinity.  If the
 169          * result is positive, behave as for {@code RoundingMode.DOWN};
 170          * if negative, behave as for {@code RoundingMode.UP}.  Note that
 171          * this rounding mode never increases the calculated value.
 172          *
 173          *<p>Example:
 174          *<table border>
 175          *<tr valign=top><th>Input Number</th>
 176          *    <th>Input rounded to one digit<br> with {@code FLOOR} rounding
 177          *<tr align=right><td>5.5</td>  <td>5</td>
 178          *<tr align=right><td>2.5</td>  <td>2</td>
 179          *<tr align=right><td>1.6</td>  <td>1</td>
 180          *<tr align=right><td>1.1</td>  <td>1</td>
 181          *<tr align=right><td>1.0</td>  <td>1</td>
 182          *<tr align=right><td>-1.0</td> <td>-1</td>
 183          *<tr align=right><td>-1.1</td> <td>-2</td>
 184          *<tr align=right><td>-1.6</td> <td>-2</td>
 185          *<tr align=right><td>-2.5</td> <td>-3</td>
 186          *<tr align=right><td>-5.5</td> <td>-6</td>
 187          *</table>
 188          */
 189     FLOOR(BigDecimal.ROUND_FLOOR),
 190 
 191         /**
 192          * Rounding mode to round towards {@literal "nearest neighbor"}
 193          * unless both neighbors are equidistant, in which case round up.
 194          * Behaves as for {@code RoundingMode.UP} if the discarded
 195          * fraction is &ge; 0.5; otherwise, behaves as for
 196          * {@code RoundingMode.DOWN}.  Note that this is the rounding
 197          * mode commonly taught at school.
 198          *
 199          *<p>Example:
 200          *<table border>
 201          *<tr valign=top><th>Input Number</th>
 202          *    <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
 203          *<tr align=right><td>5.5</td>  <td>6</td>
 204          *<tr align=right><td>2.5</td>  <td>3</td>
 205          *<tr align=right><td>1.6</td>  <td>2</td>
 206          *<tr align=right><td>1.1</td>  <td>1</td>
 207          *<tr align=right><td>1.0</td>  <td>1</td>
 208          *<tr align=right><td>-1.0</td> <td>-1</td>
 209          *<tr align=right><td>-1.1</td> <td>-1</td>
 210          *<tr align=right><td>-1.6</td> <td>-2</td>
 211          *<tr align=right><td>-2.5</td> <td>-3</td>
 212          *<tr align=right><td>-5.5</td> <td>-6</td>
 213          *</table>
 214          */
 215     HALF_UP(BigDecimal.ROUND_HALF_UP),
 216 
 217         /**
 218          * Rounding mode to round towards {@literal "nearest neighbor"}
 219          * unless both neighbors are equidistant, in which case round
 220          * down.  Behaves as for {@code RoundingMode.UP} if the discarded
 221          * fraction is &gt; 0.5; otherwise, behaves as for
 222          * {@code RoundingMode.DOWN}.
 223          *
 224          *<p>Example:
 225          *<table border>
 226          *<tr valign=top><th>Input Number</th>
 227          *    <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding
 228          *<tr align=right><td>5.5</td>  <td>5</td>
 229          *<tr align=right><td>2.5</td>  <td>2</td>
 230          *<tr align=right><td>1.6</td>  <td>2</td>
 231          *<tr align=right><td>1.1</td>  <td>1</td>
 232          *<tr align=right><td>1.0</td>  <td>1</td>
 233          *<tr align=right><td>-1.0</td> <td>-1</td>
 234          *<tr align=right><td>-1.1</td> <td>-1</td>
 235          *<tr align=right><td>-1.6</td> <td>-2</td>
 236          *<tr align=right><td>-2.5</td> <td>-2</td>
 237          *<tr align=right><td>-5.5</td> <td>-5</td>
 238          *</table>
 239          */
 240     HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
 241 
 242         /**
 243          * Rounding mode to round towards the {@literal "nearest neighbor"}
 244          * unless both neighbors are equidistant, in which case, round
 245          * towards the even neighbor.  Behaves as for
 246          * {@code RoundingMode.HALF_UP} if the digit to the left of the
 247          * discarded fraction is odd; behaves as for
 248          * {@code RoundingMode.HALF_DOWN} if it's even.  Note that this
 249          * is the rounding mode that statistically minimizes cumulative
 250          * error when applied repeatedly over a sequence of calculations.
 251          * It is sometimes known as {@literal "Banker's rounding,"} and is
 252          * chiefly used in the USA.  This rounding mode is analogous to
 253          * the rounding policy used for {@code float} and {@code double}
 254          * arithmetic in Java.
 255          *
 256          *<p>Example:
 257          *<table border>
 258          *<tr valign=top><th>Input Number</th>
 259          *    <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding
 260          *<tr align=right><td>5.5</td>  <td>6</td>
 261          *<tr align=right><td>2.5</td>  <td>2</td>
 262          *<tr align=right><td>1.6</td>  <td>2</td>
 263          *<tr align=right><td>1.1</td>  <td>1</td>
 264          *<tr align=right><td>1.0</td>  <td>1</td>
 265          *<tr align=right><td>-1.0</td> <td>-1</td>
 266          *<tr align=right><td>-1.1</td> <td>-1</td>
 267          *<tr align=right><td>-1.6</td> <td>-2</td>
 268          *<tr align=right><td>-2.5</td> <td>-2</td>
 269          *<tr align=right><td>-5.5</td> <td>-6</td>
 270          *</table>
 271          */
 272     HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
 273 
 274         /**
 275          * Rounding mode to assert that the requested operation has an exact
 276          * result, hence no rounding is necessary.  If this rounding mode is
 277          * specified on an operation that yields an inexact result, an
 278          * {@code ArithmeticException} is thrown.
 279          *<p>Example:
 280          *<table border>
 281          *<tr valign=top><th>Input Number</th>
 282          *    <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding
 283          *<tr align=right><td>5.5</td>  <td>throw {@code ArithmeticException}</td>
 284          *<tr align=right><td>2.5</td>  <td>throw {@code ArithmeticException}</td>
 285          *<tr align=right><td>1.6</td>  <td>throw {@code ArithmeticException}</td>
 286          *<tr align=right><td>1.1</td>  <td>throw {@code ArithmeticException}</td>
 287          *<tr align=right><td>1.0</td>  <td>1</td>
 288          *<tr align=right><td>-1.0</td> <td>-1</td>
 289          *<tr align=right><td>-1.1</td> <td>throw {@code ArithmeticException}</td>
 290          *<tr align=right><td>-1.6</td> <td>throw {@code ArithmeticException}</td>
 291          *<tr align=right><td>-2.5</td> <td>throw {@code ArithmeticException}</td>
 292          *<tr align=right><td>-5.5</td> <td>throw {@code ArithmeticException}</td>
 293          *</table>
 294          */
 295     UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
 296 
 297     // Corresponding BigDecimal rounding constant
 298     final int oldMode;
 299 
 300     /**
 301      * Constructor
 302      *
 303      * @param oldMode The {@code BigDecimal} constant corresponding to
 304      *        this mode
 305      */
 306     private RoundingMode(int oldMode) {
 307         this.oldMode = oldMode;
 308     }
 309 
 310     /**
 311      * Returns the {@code RoundingMode} object corresponding to a
 312      * legacy integer rounding mode constant in {@link BigDecimal}.
 313      *
 314      * @param  rm legacy integer rounding mode to convert
 315      * @return {@code RoundingMode} corresponding to the given integer.
 316      * @throws IllegalArgumentException integer is out of range
 317      */
 318     public static RoundingMode valueOf(int rm) {
 319         switch(rm) {
 320 
 321         case BigDecimal.ROUND_UP:
 322             return UP;
 323 
 324         case BigDecimal.ROUND_DOWN:
 325             return DOWN;
 326 
 327         case BigDecimal.ROUND_CEILING:
 328             return CEILING;
 329 
 330         case BigDecimal.ROUND_FLOOR:
 331             return FLOOR;
 332 
 333         case BigDecimal.ROUND_HALF_UP:
 334             return HALF_UP;
 335 
 336         case BigDecimal.ROUND_HALF_DOWN:
 337             return HALF_DOWN;
 338 
 339         case BigDecimal.ROUND_HALF_EVEN:
 340             return HALF_EVEN;
 341 
 342         case BigDecimal.ROUND_UNNECESSARY:
 343             return UNNECESSARY;
 344 
 345         default:
 346             throw new IllegalArgumentException("argument out of range");
 347         }
 348     }
 349 }