RoundingMode.java revision 3475
0N/A/*
3475N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
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 *
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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
0N/A */
0N/Apackage java.math;
0N/A
0N/A/**
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 *
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 *
0N/A *<p>
0N/A *<table border>
3475N/A * <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></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 * <tr valign=top>
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 *
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 *</table>
0N/A *
0N/A *
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 * etc. ).
0N/A *
0N/A * @see BigDecimal
0N/A * @see MathContext
0N/A * @author Josh Bloch
0N/A * @author Mike Cowlishaw
0N/A * @author Joseph D. Darcy
0N/A * @since 1.5
0N/A */
0N/Apublic enum RoundingMode {
0N/A
0N/A /**
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 * value.
0N/A *
0N/A *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A UP(BigDecimal.ROUND_UP),
0N/A
0N/A /**
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 *
0N/A *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A DOWN(BigDecimal.ROUND_DOWN),
0N/A
0N/A /**
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 *
0N/A *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A CEILING(BigDecimal.ROUND_CEILING),
0N/A
0N/A /**
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 *
0N/A *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A FLOOR(BigDecimal.ROUND_FLOOR),
0N/A
0N/A /**
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 &ge; 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 *
0N/A *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A HALF_UP(BigDecimal.ROUND_HALF_UP),
0N/A
0N/A /**
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 &gt; 0.5; otherwise, behaves as for
0N/A * {@code RoundingMode.DOWN}.
0N/A *
0N/A *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
0N/A
0N/A /**
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 *
0N/A *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
0N/A
0N/A /**
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 *<p>Example:
0N/A *<table border>
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 *</table>
0N/A */
0N/A UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
0N/A
0N/A // Corresponding BigDecimal rounding constant
0N/A final int oldMode;
0N/A
0N/A /**
0N/A * Constructor
0N/A *
0N/A * @param oldMode The {@code BigDecimal} constant corresponding to
0N/A * this mode
0N/A */
0N/A private RoundingMode(int oldMode) {
0N/A this.oldMode = oldMode;
0N/A }
0N/A
0N/A /**
0N/A * Returns the {@code RoundingMode} object corresponding to a
0N/A * legacy integer rounding mode constant in {@link BigDecimal}.
0N/A *
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
0N/A */
0N/A public static RoundingMode valueOf(int rm) {
0N/A switch(rm) {
0N/A
0N/A case BigDecimal.ROUND_UP:
0N/A return UP;
0N/A
0N/A case BigDecimal.ROUND_DOWN:
0N/A return DOWN;
0N/A
0N/A case BigDecimal.ROUND_CEILING:
0N/A return CEILING;
0N/A
0N/A case BigDecimal.ROUND_FLOOR:
0N/A return FLOOR;
0N/A
0N/A case BigDecimal.ROUND_HALF_UP:
0N/A return HALF_UP;
0N/A
0N/A case BigDecimal.ROUND_HALF_DOWN:
0N/A return HALF_DOWN;
0N/A
0N/A case BigDecimal.ROUND_HALF_EVEN:
0N/A return HALF_EVEN;
0N/A
0N/A case BigDecimal.ROUND_UNNECESSARY:
0N/A return UNNECESSARY;
0N/A
0N/A default:
0N/A throw new IllegalArgumentException("argument out of range");
0N/A }
0N/A }
0N/A}