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
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 ≥ 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 > 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 /**
|
1 /*
2 * Copyright (c) 2003, 2013, 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
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 * <caption><b>Rounding mode UP Examples</b></caption>
105 *<tr valign=top><th>Input Number</th>
106 * <th>Input rounded to one digit<br> with {@code UP} rounding
107 *<tr align=right><td>5.5</td> <td>6</td>
108 *<tr align=right><td>2.5</td> <td>3</td>
109 *<tr align=right><td>1.6</td> <td>2</td>
110 *<tr align=right><td>1.1</td> <td>2</td>
111 *<tr align=right><td>1.0</td> <td>1</td>
112 *<tr align=right><td>-1.0</td> <td>-1</td>
113 *<tr align=right><td>-1.1</td> <td>-2</td>
114 *<tr align=right><td>-1.6</td> <td>-2</td>
115 *<tr align=right><td>-2.5</td> <td>-3</td>
116 *<tr align=right><td>-5.5</td> <td>-6</td>
117 *</table>
118 */
119 UP(BigDecimal.ROUND_UP),
120
121 /**
122 * Rounding mode to round towards zero. Never increments the digit
123 * prior to a discarded fraction (i.e., truncates). Note that this
124 * rounding mode never increases the magnitude of the calculated value.
125 *
126 *<p>Example:
127 *<table border>
128 * <caption><b>Rounding mode DOWN Examples</b></caption>
129 *<tr valign=top><th>Input Number</th>
130 * <th>Input rounded to one digit<br> with {@code DOWN} rounding
131 *<tr align=right><td>5.5</td> <td>5</td>
132 *<tr align=right><td>2.5</td> <td>2</td>
133 *<tr align=right><td>1.6</td> <td>1</td>
134 *<tr align=right><td>1.1</td> <td>1</td>
135 *<tr align=right><td>1.0</td> <td>1</td>
136 *<tr align=right><td>-1.0</td> <td>-1</td>
137 *<tr align=right><td>-1.1</td> <td>-1</td>
138 *<tr align=right><td>-1.6</td> <td>-1</td>
139 *<tr align=right><td>-2.5</td> <td>-2</td>
140 *<tr align=right><td>-5.5</td> <td>-5</td>
141 *</table>
142 */
143 DOWN(BigDecimal.ROUND_DOWN),
144
145 /**
146 * Rounding mode to round towards positive infinity. If the
147 * result is positive, behaves as for {@code RoundingMode.UP};
148 * if negative, behaves as for {@code RoundingMode.DOWN}. Note
149 * that this rounding mode never decreases the calculated value.
150 *
151 *<p>Example:
152 *<table border>
153 * <caption><b>Rounding mode CEILING Examples</b></caption>
154 *<tr valign=top><th>Input Number</th>
155 * <th>Input rounded to one digit<br> with {@code CEILING} rounding
156 *<tr align=right><td>5.5</td> <td>6</td>
157 *<tr align=right><td>2.5</td> <td>3</td>
158 *<tr align=right><td>1.6</td> <td>2</td>
159 *<tr align=right><td>1.1</td> <td>2</td>
160 *<tr align=right><td>1.0</td> <td>1</td>
161 *<tr align=right><td>-1.0</td> <td>-1</td>
162 *<tr align=right><td>-1.1</td> <td>-1</td>
163 *<tr align=right><td>-1.6</td> <td>-1</td>
164 *<tr align=right><td>-2.5</td> <td>-2</td>
165 *<tr align=right><td>-5.5</td> <td>-5</td>
166 *</table>
167 */
168 CEILING(BigDecimal.ROUND_CEILING),
169
170 /**
171 * Rounding mode to round towards negative infinity. If the
172 * result is positive, behave as for {@code RoundingMode.DOWN};
173 * if negative, behave as for {@code RoundingMode.UP}. Note that
174 * this rounding mode never increases the calculated value.
175 *
176 *<p>Example:
177 *<table border>
178 * <caption><b>Rounding mode FLOOR Examples</b></caption>
179 *<tr valign=top><th>Input Number</th>
180 * <th>Input rounded to one digit<br> with {@code FLOOR} rounding
181 *<tr align=right><td>5.5</td> <td>5</td>
182 *<tr align=right><td>2.5</td> <td>2</td>
183 *<tr align=right><td>1.6</td> <td>1</td>
184 *<tr align=right><td>1.1</td> <td>1</td>
185 *<tr align=right><td>1.0</td> <td>1</td>
186 *<tr align=right><td>-1.0</td> <td>-1</td>
187 *<tr align=right><td>-1.1</td> <td>-2</td>
188 *<tr align=right><td>-1.6</td> <td>-2</td>
189 *<tr align=right><td>-2.5</td> <td>-3</td>
190 *<tr align=right><td>-5.5</td> <td>-6</td>
191 *</table>
192 */
193 FLOOR(BigDecimal.ROUND_FLOOR),
194
195 /**
196 * Rounding mode to round towards {@literal "nearest neighbor"}
197 * unless both neighbors are equidistant, in which case round up.
198 * Behaves as for {@code RoundingMode.UP} if the discarded
199 * fraction is ≥ 0.5; otherwise, behaves as for
200 * {@code RoundingMode.DOWN}. Note that this is the rounding
201 * mode commonly taught at school.
202 *
203 *<p>Example:
204 *<table border>
205 * <caption><b>Rounding mode HALF_UP Examples</b></caption>
206 *<tr valign=top><th>Input Number</th>
207 * <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
208 *<tr align=right><td>5.5</td> <td>6</td>
209 *<tr align=right><td>2.5</td> <td>3</td>
210 *<tr align=right><td>1.6</td> <td>2</td>
211 *<tr align=right><td>1.1</td> <td>1</td>
212 *<tr align=right><td>1.0</td> <td>1</td>
213 *<tr align=right><td>-1.0</td> <td>-1</td>
214 *<tr align=right><td>-1.1</td> <td>-1</td>
215 *<tr align=right><td>-1.6</td> <td>-2</td>
216 *<tr align=right><td>-2.5</td> <td>-3</td>
217 *<tr align=right><td>-5.5</td> <td>-6</td>
218 *</table>
219 */
220 HALF_UP(BigDecimal.ROUND_HALF_UP),
221
222 /**
223 * Rounding mode to round towards {@literal "nearest neighbor"}
224 * unless both neighbors are equidistant, in which case round
225 * down. Behaves as for {@code RoundingMode.UP} if the discarded
226 * fraction is > 0.5; otherwise, behaves as for
227 * {@code RoundingMode.DOWN}.
228 *
229 *<p>Example:
230 *<table border>
231 * <caption><b>Rounding mode HALF_DOWN Examples</b></caption>
232 *<tr valign=top><th>Input Number</th>
233 * <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding
234 *<tr align=right><td>5.5</td> <td>5</td>
235 *<tr align=right><td>2.5</td> <td>2</td>
236 *<tr align=right><td>1.6</td> <td>2</td>
237 *<tr align=right><td>1.1</td> <td>1</td>
238 *<tr align=right><td>1.0</td> <td>1</td>
239 *<tr align=right><td>-1.0</td> <td>-1</td>
240 *<tr align=right><td>-1.1</td> <td>-1</td>
241 *<tr align=right><td>-1.6</td> <td>-2</td>
242 *<tr align=right><td>-2.5</td> <td>-2</td>
243 *<tr align=right><td>-5.5</td> <td>-5</td>
244 *</table>
245 */
246 HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
247
248 /**
249 * Rounding mode to round towards the {@literal "nearest neighbor"}
250 * unless both neighbors are equidistant, in which case, round
251 * towards the even neighbor. Behaves as for
252 * {@code RoundingMode.HALF_UP} if the digit to the left of the
253 * discarded fraction is odd; behaves as for
254 * {@code RoundingMode.HALF_DOWN} if it's even. Note that this
255 * is the rounding mode that statistically minimizes cumulative
256 * error when applied repeatedly over a sequence of calculations.
257 * It is sometimes known as {@literal "Banker's rounding,"} and is
258 * chiefly used in the USA. This rounding mode is analogous to
259 * the rounding policy used for {@code float} and {@code double}
260 * arithmetic in Java.
261 *
262 *<p>Example:
263 *<table border>
264 * <caption><b>Rounding mode HALF_EVEN Examples</b></caption>
265 *<tr valign=top><th>Input Number</th>
266 * <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding
267 *<tr align=right><td>5.5</td> <td>6</td>
268 *<tr align=right><td>2.5</td> <td>2</td>
269 *<tr align=right><td>1.6</td> <td>2</td>
270 *<tr align=right><td>1.1</td> <td>1</td>
271 *<tr align=right><td>1.0</td> <td>1</td>
272 *<tr align=right><td>-1.0</td> <td>-1</td>
273 *<tr align=right><td>-1.1</td> <td>-1</td>
274 *<tr align=right><td>-1.6</td> <td>-2</td>
275 *<tr align=right><td>-2.5</td> <td>-2</td>
276 *<tr align=right><td>-5.5</td> <td>-6</td>
277 *</table>
278 */
279 HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
280
281 /**
282 * Rounding mode to assert that the requested operation has an exact
283 * result, hence no rounding is necessary. If this rounding mode is
284 * specified on an operation that yields an inexact result, an
285 * {@code ArithmeticException} is thrown.
286 *<p>Example:
287 *<table border>
288 * <caption><b>Rounding mode UNNECESSARY Examples</b></caption>
289 *<tr valign=top><th>Input Number</th>
290 * <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding
291 *<tr align=right><td>5.5</td> <td>throw {@code ArithmeticException}</td>
292 *<tr align=right><td>2.5</td> <td>throw {@code ArithmeticException}</td>
293 *<tr align=right><td>1.6</td> <td>throw {@code ArithmeticException}</td>
294 *<tr align=right><td>1.1</td> <td>throw {@code ArithmeticException}</td>
295 *<tr align=right><td>1.0</td> <td>1</td>
296 *<tr align=right><td>-1.0</td> <td>-1</td>
297 *<tr align=right><td>-1.1</td> <td>throw {@code ArithmeticException}</td>
298 *<tr align=right><td>-1.6</td> <td>throw {@code ArithmeticException}</td>
299 *<tr align=right><td>-2.5</td> <td>throw {@code ArithmeticException}</td>
300 *<tr align=right><td>-5.5</td> <td>throw {@code ArithmeticException}</td>
301 *</table>
302 */
303 UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
304
305 // Corresponding BigDecimal rounding constant
306 final int oldMode;
307
308 /**
|