KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > math > RoundingMode


1 /*
2  * @(#)RoundingMode.java 1.3 04/06/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * @(#)RoundingMode.java 1.x 01/xx/xx
10  *
11  * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
12  * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
13  *
14  * This software is the proprietary information of Sun Microsystems, Inc.
15  * Use is subject to license terms.
16  *
17  */

18 package java.math;
19
20 /**
21  * Specifies a <i>rounding behavior</i> for numerical operations
22  * capable of discarding precision. Each rounding mode indicates how
23  * the least significant returned digit of a rounded result is to be
24  * calculated. If fewer digits are returned than the digits needed to
25  * represent the exact numerical result, the discarded digits will be
26  * referred to as the <i>discarded fraction</i> regardless the digits'
27  * contribution to the value of the number. In other words,
28  * considered as a numerical value, the discarded fraction could have
29  * an absolute value greater than one.
30  *
31  * <p>Each rounding mode description includes a table listing how
32  * different two-digit decimal values would round to a one digit
33  * decimal value under the rounding mode in question. The result
34  * column in the tables could be gotten by creating a
35  * <tt>BigDecimal</tt> number with the specified value, forming a
36  * {@link MathContext} object with the proper settings
37  * (<tt>precision</tt> set to <tt>1</tt>, and the
38  * <tt>roundingMode</tt> set to the rounding mode in question), and
39  * calling {@link BigDecimal#round round} on this number with the
40  * proper <tt>MathContext</tt>. A summary table showing the results
41  * of these rounding operations for all rounding modes appears below.
42  *
43  *<p>
44  *<table border>
45  * <caption top><h3>Summary of Rounding Operations Under Different Rounding Modes</h3></caption>
46  * <tr><th></th><th colspan=8>Result of rounding input to one digit with the given
47  * rounding mode</th>
48  * <tr valign=top>
49  * <th>Input Number</th> <th><tt>UP</tt></th>
50  * <th><tt>DOWN</tt></th>
51  * <th><tt>CEILING</tt></th>
52  * <th><tt>FLOOR</tt></th>
53  * <th><tt>HALF_UP</tt></th>
54  * <th><tt>HALF_DOWN</tt></th>
55  * <th><tt>HALF_EVEN</tt></th>
56  * <th><tt>UNNECESSARY</tt></th>
57  *
58  * <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 <tt>ArithmeticException</tt></td>
59  * <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 <tt>ArithmeticException</tt></td>
60  * <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 <tt>ArithmeticException</tt></td>
61  * <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 <tt>ArithmeticException</tt></td>
62  * <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>
63  * <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>
64  * <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 <tt>ArithmeticException</tt></td>
65  * <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 <tt>ArithmeticException</tt></td>
66  * <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 <tt>ArithmeticException</tt></td>
67  * <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 <tt>ArithmeticException</tt></td>
68  *</table>
69  *
70  *
71  * <p>This <tt>enum</tt> is intended to replace the integer-based
72  * enumeration of rounding mode constants in {@link BigDecimal}
73  * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},
74  * etc. ).
75  *
76  * @see BigDecimal
77  * @see MathContext
78  * @version 1.x 01/xx/xx
79  * @author Josh Bloch
80  * @author Mike Cowlishaw
81  * @author Joseph D. Darcy
82  */

83 public enum RoundingMode {
84
85     /**
86      * Rounding mode to round away from zero. Always increments the
87      * digit prior to a non-zero discarded fraction. Note that this
88      * rounding mode never decreases the magnitude of the calculated
89      * value.
90      *
91      *<p>Example:
92      *<table border>
93      *<tr valign=top><th>Input Number</th>
94      * <th>Input rounded to one digit<br> with <tt>UP</tt> rounding
95      *<tr align=right><td>5.5</td> <td>6</td>
96      *<tr align=right><td>2.5</td> <td>3</td>
97      *<tr align=right><td>1.6</td> <td>2</td>
98      *<tr align=right><td>1.1</td> <td>2</td>
99      *<tr align=right><td>1.0</td> <td>1</td>
100      *<tr align=right><td>-1.0</td> <td>-1</td>
101      *<tr align=right><td>-1.1</td> <td>-2</td>
102      *<tr align=right><td>-1.6</td> <td>-2</td>
103      *<tr align=right><td>-2.5</td> <td>-3</td>
104      *<tr align=right><td>-5.5</td> <td>-6</td>
105      *</table>
106      */

107     UP(BigDecimal.ROUND_UP),
108         
109     /**
110      * Rounding mode to round towards zero. Never increments the digit
111      * prior to a discarded fraction (i.e., truncates). Note that this
112      * rounding mode never increases the magnitude of the calculated value.
113      *
114      *<p>Example:
115      *<table border>
116      *<tr valign=top><th>Input Number</th>
117      * <th>Input rounded to one digit<br> with <tt>DOWN</tt> rounding
118      *<tr align=right><td>5.5</td> <td>5</td>
119      *<tr align=right><td>2.5</td> <td>2</td>
120      *<tr align=right><td>1.6</td> <td>1</td>
121      *<tr align=right><td>1.1</td> <td>1</td>
122      *<tr align=right><td>1.0</td> <td>1</td>
123      *<tr align=right><td>-1.0</td> <td>-1</td>
124      *<tr align=right><td>-1.1</td> <td>-1</td>
125      *<tr align=right><td>-1.6</td> <td>-1</td>
126      *<tr align=right><td>-2.5</td> <td>-2</td>
127      *<tr align=right><td>-5.5</td> <td>-5</td>
128      *</table>
129      */

130     DOWN(BigDecimal.ROUND_DOWN),
131         
132     /**
133      * Rounding mode to round towards positive infinity. If the
134      * result is positive, behaves as for <tt>RoundingMode.UP</tt>;
135      * if negative, behaves as for <tt>RoundingMode.DOWN</tt>. Note
136      * that this rounding mode never decreases the calculated value.
137      *
138      *<p>Example:
139      *<table border>
140      *<tr valign=top><th>Input Number</th>
141      * <th>Input rounded to one digit<br> with <tt>CEILING</tt> rounding
142      *<tr align=right><td>5.5</td> <td>6</td>
143      *<tr align=right><td>2.5</td> <td>3</td>
144      *<tr align=right><td>1.6</td> <td>2</td>
145      *<tr align=right><td>1.1</td> <td>2</td>
146      *<tr align=right><td>1.0</td> <td>1</td>
147      *<tr align=right><td>-1.0</td> <td>-1</td>
148      *<tr align=right><td>-1.1</td> <td>-1</td>
149      *<tr align=right><td>-1.6</td> <td>-1</td>
150      *<tr align=right><td>-2.5</td> <td>-2</td>
151      *<tr align=right><td>-5.5</td> <td>-5</td>
152      *</table>
153      */

154     CEILING(BigDecimal.ROUND_CEILING),
155
156     /**
157      * Rounding mode to round towards negative infinity. If the
158      * result is positive, behave as for <tt>RoundingMode.DOWN</tt>;
159      * if negative, behave as for <tt>RoundingMode.UP</tt>. Note that
160      * this rounding mode never increases the calculated value.
161      *
162      *<p>Example:
163      *<table border>
164      *<tr valign=top><th>Input Number</th>
165      * <th>Input rounded to one digit<br> with <tt>FLOOR</tt> rounding
166      *<tr align=right><td>5.5</td> <td>5</td>
167      *<tr align=right><td>2.5</td> <td>2</td>
168      *<tr align=right><td>1.6</td> <td>1</td>
169      *<tr align=right><td>1.1</td> <td>1</td>
170      *<tr align=right><td>1.0</td> <td>1</td>
171      *<tr align=right><td>-1.0</td> <td>-1</td>
172      *<tr align=right><td>-1.1</td> <td>-2</td>
173      *<tr align=right><td>-1.6</td> <td>-2</td>
174      *<tr align=right><td>-2.5</td> <td>-3</td>
175      *<tr align=right><td>-5.5</td> <td>-6</td>
176      *</table>
177      */

178     FLOOR(BigDecimal.ROUND_FLOOR),
179
180     /**
181      * Rounding mode to round towards &quot;nearest neighbor&quot;
182      * unless both neighbors are equidistant, in which case round up.
183      * Behaves as for <tt>RoundingMode.UP</tt> if the discarded
184      * fraction is &gt;= 0.5; otherwise, behaves as for
185      * <tt>RoundingMode.DOWN</tt>. Note that this is the rounding
186      * mode commonly taught at school.
187      *
188      *<p>Example:
189      *<table border>
190      *<tr valign=top><th>Input Number</th>
191      * <th>Input rounded to one digit<br> with <tt>HALF_UP</tt> rounding
192      *<tr align=right><td>5.5</td> <td>6</td>
193      *<tr align=right><td>2.5</td> <td>3</td>
194      *<tr align=right><td>1.6</td> <td>2</td>
195      *<tr align=right><td>1.1</td> <td>1</td>
196      *<tr align=right><td>1.0</td> <td>1</td>
197      *<tr align=right><td>-1.0</td> <td>-1</td>
198      *<tr align=right><td>-1.1</td> <td>-1</td>
199      *<tr align=right><td>-1.6</td> <td>-2</td>
200      *<tr align=right><td>-2.5</td> <td>-3</td>
201      *<tr align=right><td>-5.5</td> <td>-6</td>
202      *</table>
203      */

204     HALF_UP(BigDecimal.ROUND_HALF_UP),
205
206     /**
207      * Rounding mode to round towards &quot;nearest neighbor&quot;
208      * unless both neighbors are equidistant, in which case round
209      * down. Behaves as for <tt>RoundingMode.UP</tt> if the discarded
210      * fraction is &gt; 0.5; otherwise, behaves as for
211      * <tt>RoundingMode.DOWN</tt>.
212      *
213      *<p>Example:
214      *<table border>
215      *<tr valign=top><th>Input Number</th>
216      * <th>Input rounded to one digit<br> with <tt>HALF_DOWN</tt> rounding
217      *<tr align=right><td>5.5</td> <td>5</td>
218      *<tr align=right><td>2.5</td> <td>2</td>
219      *<tr align=right><td>1.6</td> <td>2</td>
220      *<tr align=right><td>1.1</td> <td>1</td>
221      *<tr align=right><td>1.0</td> <td>1</td>
222      *<tr align=right><td>-1.0</td> <td>-1</td>
223      *<tr align=right><td>-1.1</td> <td>-1</td>
224      *<tr align=right><td>-1.6</td> <td>-2</td>
225      *<tr align=right><td>-2.5</td> <td>-2</td>
226      *<tr align=right><td>-5.5</td> <td>-5</td>
227      *</table>
228      */

229     HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
230
231     /**
232      * Rounding mode to round towards the &quot;nearest neighbor&quot;
233      * unless both neighbors are equidistant, in which case, round
234      * towards the even neighbor. Behaves as for
235      * <tt>RoundingMode.HALF_UP</tt> if the digit to the left of the
236      * discarded fraction is odd; behaves as for
237      * <tt>RoundingMode.HALF_DOWN</tt> if it's even. Note that this
238      * is the rounding mode that statistically minimizes cumulative
239      * error when applied repeatedly over a sequence of calculations.
240      * It is sometimes known as &quot;Banker's rounding,&quot; and is
241      * chiefly used in the USA. This rounding mode is analogous to
242      * the rounding policy used for <tt>float</tt> and <tt>double</tt>
243      * arithmetic in Java.
244      *
245      *<p>Example:
246      *<table border>
247      *<tr valign=top><th>Input Number</th>
248      * <th>Input rounded to one digit<br> with <tt>HALF_EVEN</tt> rounding
249      *<tr align=right><td>5.5</td> <td>6</td>
250      *<tr align=right><td>2.5</td> <td>2</td>
251      *<tr align=right><td>1.6</td> <td>2</td>
252      *<tr align=right><td>1.1</td> <td>1</td>
253      *<tr align=right><td>1.0</td> <td>1</td>
254      *<tr align=right><td>-1.0</td> <td>-1</td>
255      *<tr align=right><td>-1.1</td> <td>-1</td>
256      *<tr align=right><td>-1.6</td> <td>-2</td>
257      *<tr align=right><td>-2.5</td> <td>-2</td>
258      *<tr align=right><td>-5.5</td> <td>-6</td>
259      *</table>
260      */

261     HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
262
263     /**
264      * Rounding mode to assert that the requested operation has an exact
265      * result, hence no rounding is necessary. If this rounding mode is
266      * specified on an operation that yields an inexact result, an
267      * <tt>ArithmeticException</tt> is thrown.
268      *<p>Example:
269      *<table border>
270      *<tr valign=top><th>Input Number</th>
271      * <th>Input rounded to one digit<br> with <tt>UNNECESSARY</tt> rounding
272      *<tr align=right><td>5.5</td> <td>throw <tt>ArithmeticException</tt></td>
273      *<tr align=right><td>2.5</td> <td>throw <tt>ArithmeticException</tt></td>
274      *<tr align=right><td>1.6</td> <td>throw <tt>ArithmeticException</tt></td>
275      *<tr align=right><td>1.1</td> <td>throw <tt>ArithmeticException</tt></td>
276      *<tr align=right><td>1.0</td> <td>1</td>
277      *<tr align=right><td>-1.0</td> <td>-1</td>
278      *<tr align=right><td>-1.1</td> <td>throw <tt>ArithmeticException</tt></td>
279      *<tr align=right><td>-1.6</td> <td>throw <tt>ArithmeticException</tt></td>
280      *<tr align=right><td>-2.5</td> <td>throw <tt>ArithmeticException</tt></td>
281      *<tr align=right><td>-5.5</td> <td>throw <tt>ArithmeticException</tt></td>
282      *</table>
283      */

284     UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
285
286     // Corresponding BigDecimal rounding constant
287
final int oldMode;
288
289     /**
290      * Constructor
291      *
292      * @param oldMode The <tt>BigDecimal</tt> constant corresponding to
293      * this mode
294      */

295     private RoundingMode(int oldMode) {
296         this.oldMode = oldMode;
297     }
298
299     /**
300      * Returns the <tt>RoundingMode</tt> object corresponding to a
301      * legacy integer rounding mode constant in {@link BigDecimal}.
302      *
303      * @param rm legacy integer rounding mode to convert
304      * @return <tt>RoundingMode</tt> corresponding to the given integer.
305      * @throws IllegalArgumentException integer is out of range
306      */

307     public static RoundingMode JavaDoc valueOf(int rm) {
308     switch(rm) {
309
310     case BigDecimal.ROUND_UP:
311         return UP;
312
313     case BigDecimal.ROUND_DOWN:
314         return DOWN;
315
316     case BigDecimal.ROUND_CEILING:
317         return CEILING;
318         
319     case BigDecimal.ROUND_FLOOR:
320         return FLOOR;
321
322     case BigDecimal.ROUND_HALF_UP:
323         return HALF_UP;
324         
325     case BigDecimal.ROUND_HALF_DOWN:
326         return HALF_DOWN;
327         
328     case BigDecimal.ROUND_HALF_EVEN:
329         return HALF_EVEN;
330         
331     case BigDecimal.ROUND_UNNECESSARY:
332         return UNNECESSARY;
333         
334     default:
335         throw new IllegalArgumentException JavaDoc("argument out of range");
336     }
337     }
338 }
339
Popular Tags