jaroslav@1258: /* jaroslav@1258: * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@1258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1258: * jaroslav@1258: * This code is free software; you can redistribute it and/or modify it jaroslav@1258: * under the terms of the GNU General Public License version 2 only, as jaroslav@1258: * published by the Free Software Foundation. Oracle designates this jaroslav@1258: * particular file as subject to the "Classpath" exception as provided jaroslav@1258: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1258: * jaroslav@1258: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1258: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1258: * accompanied this code). jaroslav@1258: * jaroslav@1258: * You should have received a copy of the GNU General Public License version jaroslav@1258: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1258: * jaroslav@1258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1258: * or visit www.oracle.com if you need additional information or have any jaroslav@1258: * questions. jaroslav@1258: */ jaroslav@1258: jaroslav@1258: /* jaroslav@1258: * Portions Copyright IBM Corporation, 2001. All Rights Reserved. jaroslav@1258: */ jaroslav@1258: package java.math; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Specifies a rounding behavior for numerical operations jaroslav@1258: * capable of discarding precision. Each rounding mode indicates how jaroslav@1258: * the least significant returned digit of a rounded result is to be jaroslav@1258: * calculated. If fewer digits are returned than the digits needed to jaroslav@1258: * represent the exact numerical result, the discarded digits will be jaroslav@1258: * referred to as the discarded fraction regardless the digits' jaroslav@1258: * contribution to the value of the number. In other words, jaroslav@1258: * considered as a numerical value, the discarded fraction could have jaroslav@1258: * an absolute value greater than one. jaroslav@1258: * jaroslav@1258: *
Each rounding mode description includes a table listing how jaroslav@1258: * different two-digit decimal values would round to a one digit jaroslav@1258: * decimal value under the rounding mode in question. The result jaroslav@1258: * column in the tables could be gotten by creating a jaroslav@1258: * {@code BigDecimal} number with the specified value, forming a jaroslav@1258: * {@link MathContext} object with the proper settings jaroslav@1258: * ({@code precision} set to {@code 1}, and the jaroslav@1258: * {@code roundingMode} set to the rounding mode in question), and jaroslav@1258: * calling {@link BigDecimal#round round} on this number with the jaroslav@1258: * proper {@code MathContext}. A summary table showing the results jaroslav@1258: * of these rounding operations for all rounding modes appears below. jaroslav@1258: * jaroslav@1258: *
jaroslav@1258: *
Result of rounding input to one digit with the given jaroslav@1258: * rounding mode | jaroslav@1258: *||||||||
---|---|---|---|---|---|---|---|---|
Input Number | {@code UP} | jaroslav@1258: *{@code DOWN} | jaroslav@1258: *{@code CEILING} | jaroslav@1258: *{@code FLOOR} | jaroslav@1258: *{@code HALF_UP} | jaroslav@1258: *{@code HALF_DOWN} | jaroslav@1258: *{@code HALF_EVEN} | jaroslav@1258: *{@code UNNECESSARY} | jaroslav@1258: * jaroslav@1258: *
5.5 | 6 | 5 | 6 | 5 | 6 | 5 | 6 | throw {@code ArithmeticException} | jaroslav@1258: *
2.5 | 3 | 2 | 3 | 2 | 3 | 2 | 2 | throw {@code ArithmeticException} | jaroslav@1258: *
1.6 | 2 | 1 | 2 | 1 | 2 | 2 | 2 | throw {@code ArithmeticException} | jaroslav@1258: *
1.1 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | throw {@code ArithmeticException} | jaroslav@1258: *
1.0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | jaroslav@1258: *
-1.0 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | jaroslav@1258: *
-1.1 | -2 | -1 | -1 | -2 | -1 | -1 | -1 | throw {@code ArithmeticException} | jaroslav@1258: *
-1.6 | -2 | -1 | -1 | -2 | -2 | -2 | -2 | throw {@code ArithmeticException} | jaroslav@1258: *
-2.5 | -3 | -2 | -2 | -3 | -3 | -2 | -2 | throw {@code ArithmeticException} | jaroslav@1258: *
-5.5 | -6 | -5 | -5 | -6 | -6 | -5 | -6 | throw {@code ArithmeticException} | jaroslav@1258: *
This {@code enum} is intended to replace the integer-based jaroslav@1258: * enumeration of rounding mode constants in {@link BigDecimal} jaroslav@1258: * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN}, jaroslav@1258: * etc. ). jaroslav@1258: * jaroslav@1258: * @see BigDecimal jaroslav@1258: * @see MathContext jaroslav@1258: * @author Josh Bloch jaroslav@1258: * @author Mike Cowlishaw jaroslav@1258: * @author Joseph D. Darcy jaroslav@1258: * @since 1.5 jaroslav@1258: */ jaroslav@1258: public enum RoundingMode { jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to round away from zero. Always increments the jaroslav@1258: * digit prior to a non-zero discarded fraction. Note that this jaroslav@1258: * rounding mode never decreases the magnitude of the calculated jaroslav@1258: * value. jaroslav@1258: * jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code UP} rounding jaroslav@1258: * |
---|---|
5.5 | 6 | jaroslav@1258: *
2.5 | 3 | jaroslav@1258: *
1.6 | 2 | jaroslav@1258: *
1.1 | 2 | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | -2 | jaroslav@1258: *
-1.6 | -2 | jaroslav@1258: *
-2.5 | -3 | jaroslav@1258: *
-5.5 | -6 | jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code DOWN} rounding jaroslav@1258: * |
---|---|
5.5 | 5 | jaroslav@1258: *
2.5 | 2 | jaroslav@1258: *
1.6 | 1 | jaroslav@1258: *
1.1 | 1 | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | -1 | jaroslav@1258: *
-1.6 | -1 | jaroslav@1258: *
-2.5 | -2 | jaroslav@1258: *
-5.5 | -5 | jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code CEILING} rounding jaroslav@1258: * |
---|---|
5.5 | 6 | jaroslav@1258: *
2.5 | 3 | jaroslav@1258: *
1.6 | 2 | jaroslav@1258: *
1.1 | 2 | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | -1 | jaroslav@1258: *
-1.6 | -1 | jaroslav@1258: *
-2.5 | -2 | jaroslav@1258: *
-5.5 | -5 | jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code FLOOR} rounding jaroslav@1258: * |
---|---|
5.5 | 5 | jaroslav@1258: *
2.5 | 2 | jaroslav@1258: *
1.6 | 1 | jaroslav@1258: *
1.1 | 1 | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | -2 | jaroslav@1258: *
-1.6 | -2 | jaroslav@1258: *
-2.5 | -3 | jaroslav@1258: *
-5.5 | -6 | jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code HALF_UP} rounding jaroslav@1258: * |
---|---|
5.5 | 6 | jaroslav@1258: *
2.5 | 3 | jaroslav@1258: *
1.6 | 2 | jaroslav@1258: *
1.1 | 1 | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | -1 | jaroslav@1258: *
-1.6 | -2 | jaroslav@1258: *
-2.5 | -3 | jaroslav@1258: *
-5.5 | -6 | jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code HALF_DOWN} rounding jaroslav@1258: * |
---|---|
5.5 | 5 | jaroslav@1258: *
2.5 | 2 | jaroslav@1258: *
1.6 | 2 | jaroslav@1258: *
1.1 | 1 | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | -1 | jaroslav@1258: *
-1.6 | -2 | jaroslav@1258: *
-2.5 | -2 | jaroslav@1258: *
-5.5 | -5 | jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code HALF_EVEN} rounding jaroslav@1258: * |
---|---|
5.5 | 6 | jaroslav@1258: *
2.5 | 2 | jaroslav@1258: *
1.6 | 2 | jaroslav@1258: *
1.1 | 1 | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | -1 | jaroslav@1258: *
-1.6 | -2 | jaroslav@1258: *
-2.5 | -2 | jaroslav@1258: *
-5.5 | -6 | jaroslav@1258: *
Example: jaroslav@1258: *
Input Number | jaroslav@1258: *Input rounded to one digit with {@code UNNECESSARY} rounding jaroslav@1258: * |
---|---|
5.5 | throw {@code ArithmeticException} | jaroslav@1258: *
2.5 | throw {@code ArithmeticException} | jaroslav@1258: *
1.6 | throw {@code ArithmeticException} | jaroslav@1258: *
1.1 | throw {@code ArithmeticException} | jaroslav@1258: *
1.0 | 1 | jaroslav@1258: *
-1.0 | -1 | jaroslav@1258: *
-1.1 | throw {@code ArithmeticException} | jaroslav@1258: *
-1.6 | throw {@code ArithmeticException} | jaroslav@1258: *
-2.5 | throw {@code ArithmeticException} | jaroslav@1258: *
-5.5 | throw {@code ArithmeticException} | jaroslav@1258: *