RoundingMode.java revision 0
0N/A * Portions Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * Portions Copyright IBM Corporation, 2001. All Rights Reserved. 0N/A * Specifies a <i>rounding behavior</i> for numerical operations 0N/A * capable of discarding precision. Each rounding mode indicates how 0N/A * the least significant returned digit of a rounded result is to be 0N/A * calculated. If fewer digits are returned than the digits needed to 0N/A * represent the exact numerical result, the discarded digits will be 0N/A * referred to as the <i>discarded fraction</i> regardless the digits' 0N/A * contribution to the value of the number. In other words, 0N/A * considered as a numerical value, the discarded fraction could have 0N/A * an absolute value greater than one. 0N/A * <p>Each rounding mode description includes a table listing how 0N/A * different two-digit decimal values would round to a one digit 0N/A * decimal value under the rounding mode in question. The result 0N/A * column in the tables could be gotten by creating a 0N/A * {@code BigDecimal} number with the specified value, forming a 0N/A * {@link MathContext} object with the proper settings 0N/A * ({@code precision} set to {@code 1}, and the 0N/A * {@code roundingMode} set to the rounding mode in question), and 0N/A * calling {@link BigDecimal#round round} on this number with the 0N/A * proper {@code MathContext}. A summary table showing the results 0N/A * of these rounding operations for all rounding modes appears below. 0N/A * <caption top><h3>Summary of Rounding Operations Under Different Rounding Modes</h3></caption> 0N/A * <tr><th></th><th colspan=8>Result of rounding input to one digit with the given 0N/A * rounding mode</th> 0N/A * <th>Input Number</th> <th>{@code UP}</th> 0N/A * <th>{@code DOWN}</th> 0N/A * <th>{@code CEILING}</th> 0N/A * <th>{@code FLOOR}</th> 0N/A * <th>{@code HALF_UP}</th> 0N/A * <th>{@code HALF_DOWN}</th> 0N/A * <th>{@code HALF_EVEN}</th> 0N/A * <th>{@code UNNECESSARY}</th> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <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> 0N/A * <p>This {@code enum} is intended to replace the integer-based 0N/A * enumeration of rounding mode constants in {@link BigDecimal} 0N/A * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN}, 0N/A * @author Josh Bloch 0N/A * @author Mike Cowlishaw 0N/A * @author Joseph D. Darcy 0N/A * Rounding mode to round away from zero. Always increments the 0N/A * digit prior to a non-zero discarded fraction. Note that this 0N/A * rounding mode never decreases the magnitude of the calculated 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code UP} rounding 0N/A *<tr align=right><td>5.5</td> <td>6</td> 0N/A *<tr align=right><td>2.5</td> <td>3</td> 0N/A *<tr align=right><td>1.6</td> <td>2</td> 0N/A *<tr align=right><td>1.1</td> <td>2</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>-2</td> 0N/A *<tr align=right><td>-1.6</td> <td>-2</td> 0N/A *<tr align=right><td>-2.5</td> <td>-3</td> 0N/A *<tr align=right><td>-5.5</td> <td>-6</td> 0N/A * Rounding mode to round towards zero. Never increments the digit 0N/A * prior to a discarded fraction (i.e., truncates). Note that this 0N/A * rounding mode never increases the magnitude of the calculated value. 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code DOWN} rounding 0N/A *<tr align=right><td>5.5</td> <td>5</td> 0N/A *<tr align=right><td>2.5</td> <td>2</td> 0N/A *<tr align=right><td>1.6</td> <td>1</td> 0N/A *<tr align=right><td>1.1</td> <td>1</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>-1</td> 0N/A *<tr align=right><td>-1.6</td> <td>-1</td> 0N/A *<tr align=right><td>-2.5</td> <td>-2</td> 0N/A *<tr align=right><td>-5.5</td> <td>-5</td> 0N/A * Rounding mode to round towards positive infinity. If the 0N/A * result is positive, behaves as for {@code RoundingMode.UP}; 0N/A * if negative, behaves as for {@code RoundingMode.DOWN}. Note 0N/A * that this rounding mode never decreases the calculated value. 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code CEILING} rounding 0N/A *<tr align=right><td>5.5</td> <td>6</td> 0N/A *<tr align=right><td>2.5</td> <td>3</td> 0N/A *<tr align=right><td>1.6</td> <td>2</td> 0N/A *<tr align=right><td>1.1</td> <td>2</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>-1</td> 0N/A *<tr align=right><td>-1.6</td> <td>-1</td> 0N/A *<tr align=right><td>-2.5</td> <td>-2</td> 0N/A *<tr align=right><td>-5.5</td> <td>-5</td> 0N/A * Rounding mode to round towards negative infinity. If the 0N/A * result is positive, behave as for {@code RoundingMode.DOWN}; 0N/A * if negative, behave as for {@code RoundingMode.UP}. Note that 0N/A * this rounding mode never increases the calculated value. 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code FLOOR} rounding 0N/A *<tr align=right><td>5.5</td> <td>5</td> 0N/A *<tr align=right><td>2.5</td> <td>2</td> 0N/A *<tr align=right><td>1.6</td> <td>1</td> 0N/A *<tr align=right><td>1.1</td> <td>1</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>-2</td> 0N/A *<tr align=right><td>-1.6</td> <td>-2</td> 0N/A *<tr align=right><td>-2.5</td> <td>-3</td> 0N/A *<tr align=right><td>-5.5</td> <td>-6</td> 0N/A * Rounding mode to round towards {@literal "nearest neighbor"} 0N/A * unless both neighbors are equidistant, in which case round up. 0N/A * Behaves as for {@code RoundingMode.UP} if the discarded 0N/A * fraction is ≥ 0.5; otherwise, behaves as for 0N/A * {@code RoundingMode.DOWN}. Note that this is the rounding 0N/A * mode commonly taught at school. 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code HALF_UP} rounding 0N/A *<tr align=right><td>5.5</td> <td>6</td> 0N/A *<tr align=right><td>2.5</td> <td>3</td> 0N/A *<tr align=right><td>1.6</td> <td>2</td> 0N/A *<tr align=right><td>1.1</td> <td>1</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>-1</td> 0N/A *<tr align=right><td>-1.6</td> <td>-2</td> 0N/A *<tr align=right><td>-2.5</td> <td>-3</td> 0N/A *<tr align=right><td>-5.5</td> <td>-6</td> 0N/A * Rounding mode to round towards {@literal "nearest neighbor"} 0N/A * unless both neighbors are equidistant, in which case round 0N/A * down. Behaves as for {@code RoundingMode.UP} if the discarded 0N/A * fraction is > 0.5; otherwise, behaves as for 0N/A * {@code RoundingMode.DOWN}. 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding 0N/A *<tr align=right><td>5.5</td> <td>5</td> 0N/A *<tr align=right><td>2.5</td> <td>2</td> 0N/A *<tr align=right><td>1.6</td> <td>2</td> 0N/A *<tr align=right><td>1.1</td> <td>1</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>-1</td> 0N/A *<tr align=right><td>-1.6</td> <td>-2</td> 0N/A *<tr align=right><td>-2.5</td> <td>-2</td> 0N/A *<tr align=right><td>-5.5</td> <td>-5</td> 0N/A * Rounding mode to round towards the {@literal "nearest neighbor"} 0N/A * unless both neighbors are equidistant, in which case, round 0N/A * towards the even neighbor. Behaves as for 0N/A * {@code RoundingMode.HALF_UP} if the digit to the left of the 0N/A * discarded fraction is odd; behaves as for 0N/A * {@code RoundingMode.HALF_DOWN} if it's even. Note that this 0N/A * is the rounding mode that statistically minimizes cumulative 0N/A * error when applied repeatedly over a sequence of calculations. 0N/A * It is sometimes known as {@literal "Banker's rounding,"} and is 0N/A * chiefly used in the USA. This rounding mode is analogous to 0N/A * the rounding policy used for {@code float} and {@code double} 0N/A * arithmetic in Java. 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding 0N/A *<tr align=right><td>5.5</td> <td>6</td> 0N/A *<tr align=right><td>2.5</td> <td>2</td> 0N/A *<tr align=right><td>1.6</td> <td>2</td> 0N/A *<tr align=right><td>1.1</td> <td>1</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>-1</td> 0N/A *<tr align=right><td>-1.6</td> <td>-2</td> 0N/A *<tr align=right><td>-2.5</td> <td>-2</td> 0N/A *<tr align=right><td>-5.5</td> <td>-6</td> 0N/A * Rounding mode to assert that the requested operation has an exact 0N/A * result, hence no rounding is necessary. If this rounding mode is 0N/A * specified on an operation that yields an inexact result, an 0N/A * {@code ArithmeticException} is thrown. 0N/A *<tr valign=top><th>Input Number</th> 0N/A * <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding 0N/A *<tr align=right><td>5.5</td> <td>throw {@code ArithmeticException}</td> 0N/A *<tr align=right><td>2.5</td> <td>throw {@code ArithmeticException}</td> 0N/A *<tr align=right><td>1.6</td> <td>throw {@code ArithmeticException}</td> 0N/A *<tr align=right><td>1.1</td> <td>throw {@code ArithmeticException}</td> 0N/A *<tr align=right><td>1.0</td> <td>1</td> 0N/A *<tr align=right><td>-1.0</td> <td>-1</td> 0N/A *<tr align=right><td>-1.1</td> <td>throw {@code ArithmeticException}</td> 0N/A *<tr align=right><td>-1.6</td> <td>throw {@code ArithmeticException}</td> 0N/A *<tr align=right><td>-2.5</td> <td>throw {@code ArithmeticException}</td> 0N/A *<tr align=right><td>-5.5</td> <td>throw {@code ArithmeticException}</td> 0N/A // Corresponding BigDecimal rounding constant 0N/A * @param oldMode The {@code BigDecimal} constant corresponding to 0N/A * Returns the {@code RoundingMode} object corresponding to a 0N/A * legacy integer rounding mode constant in {@link BigDecimal}. 0N/A * @param rm legacy integer rounding mode to convert 0N/A * @return {@code RoundingMode} corresponding to the given integer. 0N/A * @throws IllegalArgumentException integer is out of range