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: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Summary of Rounding Operations Under Different Rounding Modes
Result of rounding input to one digit with the given jaroslav@1258: * rounding mode
Input Number {@code UP}{@code DOWN}{@code CEILING}{@code FLOOR}{@code HALF_UP}{@code HALF_DOWN}{@code HALF_EVEN}{@code UNNECESSARY}
5.5 6 5 6 5 6 5 6 throw {@code ArithmeticException}
2.5 3 2 3 2 3 2 2 throw {@code ArithmeticException}
1.6 2 1 2 1 2 2 2 throw {@code ArithmeticException}
1.1 2 1 2 1 1 1 1 throw {@code ArithmeticException}
1.0 1 1 1 1 1 1 1 1
-1.0 -1 -1 -1 -1 -1 -1 -1 -1
-1.1 -2 -1 -1 -2 -1 -1 -1 throw {@code ArithmeticException}
-1.6 -2 -1 -1 -2 -2 -2 -2 throw {@code ArithmeticException}
-2.5 -3 -2 -2 -3 -3 -2 -2 throw {@code ArithmeticException}
-5.5 -6 -5 -5 -6 -6 -5 -6 throw {@code ArithmeticException}
jaroslav@1258: * jaroslav@1258: * 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: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code UP} rounding jaroslav@1258: *
5.5 6
2.5 3
1.6 2
1.1 2
1.0 1
-1.0 -1
-1.1 -2
-1.6 -2
-2.5 -3
-5.5 -6
jaroslav@1258: */ jaroslav@1258: UP(BigDecimal.ROUND_UP), jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to round towards zero. Never increments the digit jaroslav@1258: * prior to a discarded fraction (i.e., truncates). Note that this jaroslav@1258: * rounding mode never increases the magnitude of the calculated value. jaroslav@1258: * jaroslav@1258: *

Example: jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code DOWN} rounding jaroslav@1258: *
5.5 5
2.5 2
1.6 1
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -1
-2.5 -2
-5.5 -5
jaroslav@1258: */ jaroslav@1258: DOWN(BigDecimal.ROUND_DOWN), jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to round towards positive infinity. If the jaroslav@1258: * result is positive, behaves as for {@code RoundingMode.UP}; jaroslav@1258: * if negative, behaves as for {@code RoundingMode.DOWN}. Note jaroslav@1258: * that this rounding mode never decreases the calculated value. jaroslav@1258: * jaroslav@1258: *

Example: jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code CEILING} rounding jaroslav@1258: *
5.5 6
2.5 3
1.6 2
1.1 2
1.0 1
-1.0 -1
-1.1 -1
-1.6 -1
-2.5 -2
-5.5 -5
jaroslav@1258: */ jaroslav@1258: CEILING(BigDecimal.ROUND_CEILING), jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to round towards negative infinity. If the jaroslav@1258: * result is positive, behave as for {@code RoundingMode.DOWN}; jaroslav@1258: * if negative, behave as for {@code RoundingMode.UP}. Note that jaroslav@1258: * this rounding mode never increases the calculated value. jaroslav@1258: * jaroslav@1258: *

Example: jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code FLOOR} rounding jaroslav@1258: *
5.5 5
2.5 2
1.6 1
1.1 1
1.0 1
-1.0 -1
-1.1 -2
-1.6 -2
-2.5 -3
-5.5 -6
jaroslav@1258: */ jaroslav@1258: FLOOR(BigDecimal.ROUND_FLOOR), jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to round towards {@literal "nearest neighbor"} jaroslav@1258: * unless both neighbors are equidistant, in which case round up. jaroslav@1258: * Behaves as for {@code RoundingMode.UP} if the discarded jaroslav@1258: * fraction is ≥ 0.5; otherwise, behaves as for jaroslav@1258: * {@code RoundingMode.DOWN}. Note that this is the rounding jaroslav@1258: * mode commonly taught at school. jaroslav@1258: * jaroslav@1258: *

Example: jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code HALF_UP} rounding jaroslav@1258: *
5.5 6
2.5 3
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -3
-5.5 -6
jaroslav@1258: */ jaroslav@1258: HALF_UP(BigDecimal.ROUND_HALF_UP), jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to round towards {@literal "nearest neighbor"} jaroslav@1258: * unless both neighbors are equidistant, in which case round jaroslav@1258: * down. Behaves as for {@code RoundingMode.UP} if the discarded jaroslav@1258: * fraction is > 0.5; otherwise, behaves as for jaroslav@1258: * {@code RoundingMode.DOWN}. jaroslav@1258: * jaroslav@1258: *

Example: jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code HALF_DOWN} rounding jaroslav@1258: *
5.5 5
2.5 2
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -5
jaroslav@1258: */ jaroslav@1258: HALF_DOWN(BigDecimal.ROUND_HALF_DOWN), jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to round towards the {@literal "nearest neighbor"} jaroslav@1258: * unless both neighbors are equidistant, in which case, round jaroslav@1258: * towards the even neighbor. Behaves as for jaroslav@1258: * {@code RoundingMode.HALF_UP} if the digit to the left of the jaroslav@1258: * discarded fraction is odd; behaves as for jaroslav@1258: * {@code RoundingMode.HALF_DOWN} if it's even. Note that this jaroslav@1258: * is the rounding mode that statistically minimizes cumulative jaroslav@1258: * error when applied repeatedly over a sequence of calculations. jaroslav@1258: * It is sometimes known as {@literal "Banker's rounding,"} and is jaroslav@1258: * chiefly used in the USA. This rounding mode is analogous to jaroslav@1258: * the rounding policy used for {@code float} and {@code double} jaroslav@1258: * arithmetic in Java. jaroslav@1258: * jaroslav@1258: *

Example: jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code HALF_EVEN} rounding jaroslav@1258: *
5.5 6
2.5 2
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -6
jaroslav@1258: */ jaroslav@1258: HALF_EVEN(BigDecimal.ROUND_HALF_EVEN), jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Rounding mode to assert that the requested operation has an exact jaroslav@1258: * result, hence no rounding is necessary. If this rounding mode is jaroslav@1258: * specified on an operation that yields an inexact result, an jaroslav@1258: * {@code ArithmeticException} is thrown. jaroslav@1258: *

Example: jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: * jaroslav@1258: *
Input NumberInput rounded to one digit
with {@code UNNECESSARY} rounding jaroslav@1258: *
5.5 throw {@code ArithmeticException}
2.5 throw {@code ArithmeticException}
1.6 throw {@code ArithmeticException}
1.1 throw {@code ArithmeticException}
1.0 1
-1.0 -1
-1.1 throw {@code ArithmeticException}
-1.6 throw {@code ArithmeticException}
-2.5 throw {@code ArithmeticException}
-5.5 throw {@code ArithmeticException}
jaroslav@1258: */ jaroslav@1258: UNNECESSARY(BigDecimal.ROUND_UNNECESSARY); jaroslav@1258: jaroslav@1258: // Corresponding BigDecimal rounding constant jaroslav@1258: final int oldMode; jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Constructor jaroslav@1258: * jaroslav@1258: * @param oldMode The {@code BigDecimal} constant corresponding to jaroslav@1258: * this mode jaroslav@1258: */ jaroslav@1258: private RoundingMode(int oldMode) { jaroslav@1258: this.oldMode = oldMode; jaroslav@1258: } jaroslav@1258: jaroslav@1258: /** jaroslav@1258: * Returns the {@code RoundingMode} object corresponding to a jaroslav@1258: * legacy integer rounding mode constant in {@link BigDecimal}. jaroslav@1258: * jaroslav@1258: * @param rm legacy integer rounding mode to convert jaroslav@1258: * @return {@code RoundingMode} corresponding to the given integer. jaroslav@1258: * @throws IllegalArgumentException integer is out of range jaroslav@1258: */ jaroslav@1258: public static RoundingMode valueOf(int rm) { jaroslav@1258: switch(rm) { jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_UP: jaroslav@1258: return UP; jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_DOWN: jaroslav@1258: return DOWN; jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_CEILING: jaroslav@1258: return CEILING; jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_FLOOR: jaroslav@1258: return FLOOR; jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_HALF_UP: jaroslav@1258: return HALF_UP; jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_HALF_DOWN: jaroslav@1258: return HALF_DOWN; jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_HALF_EVEN: jaroslav@1258: return HALF_EVEN; jaroslav@1258: jaroslav@1258: case BigDecimal.ROUND_UNNECESSARY: jaroslav@1258: return UNNECESSARY; jaroslav@1258: jaroslav@1258: default: jaroslav@1258: throw new IllegalArgumentException("argument out of range"); jaroslav@1258: } jaroslav@1258: } jaroslav@1258: }