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. * @(#)RoundingMode.java 1.x 01/xx/xx
  9. *
  10. * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
  11. * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
  12. *
  13. * This software is the proprietary information of Sun Microsystems, Inc.
  14. * Use is subject to license terms.
  15. *
  16. */
  17. package java.math;
  18. /**
  19. * Specifies a <i>rounding behavior</i> for numerical operations
  20. * capable of discarding precision. Each rounding mode indicates how
  21. * the least significant returned digit of a rounded result is to be
  22. * calculated. If fewer digits are returned than the digits needed to
  23. * represent the exact numerical result, the discarded digits will be
  24. * referred to as the <i>discarded fraction</i> regardless the digits'
  25. * contribution to the value of the number. In other words,
  26. * considered as a numerical value, the discarded fraction could have
  27. * an absolute value greater than one.
  28. *
  29. * <p>Each rounding mode description includes a table listing how
  30. * different two-digit decimal values would round to a one digit
  31. * decimal value under the rounding mode in question. The result
  32. * column in the tables could be gotten by creating a
  33. * <tt>BigDecimal</tt> number with the specified value, forming a
  34. * {@link MathContext} object with the proper settings
  35. * (<tt>precision</tt> set to <tt>1</tt>, and the
  36. * <tt>roundingMode</tt> set to the rounding mode in question), and
  37. * calling {@link BigDecimal#round round} on this number with the
  38. * proper <tt>MathContext</tt>. A summary table showing the results
  39. * of these rounding operations for all rounding modes appears below.
  40. *
  41. *<p>
  42. *<table border>
  43. * <caption top><h3>Summary of Rounding Operations Under Different Rounding Modes</h3></caption>
  44. * <tr><th></th><th colspan=8>Result of rounding input to one digit with the given
  45. * rounding mode</th>
  46. * <tr valign=top>
  47. * <th>Input Number</th> <th><tt>UP</tt></th>
  48. * <th><tt>DOWN</tt></th>
  49. * <th><tt>CEILING</tt></th>
  50. * <th><tt>FLOOR</tt></th>
  51. * <th><tt>HALF_UP</tt></th>
  52. * <th><tt>HALF_DOWN</tt></th>
  53. * <th><tt>HALF_EVEN</tt></th>
  54. * <th><tt>UNNECESSARY</tt></th>
  55. *
  56. * <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>
  57. * <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>
  58. * <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>
  59. * <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>
  60. * <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>
  61. * <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>
  62. * <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>
  63. * <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>
  64. * <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>
  65. * <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>
  66. *</table>
  67. *
  68. *
  69. * <p>This <tt>enum</tt> is intended to replace the integer-based
  70. * enumeration of rounding mode constants in {@link BigDecimal}
  71. * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},
  72. * etc. ).
  73. *
  74. * @see BigDecimal
  75. * @see MathContext
  76. * @version 1.x 01/xx/xx
  77. * @author Josh Bloch
  78. * @author Mike Cowlishaw
  79. * @author Joseph D. Darcy
  80. */
  81. public enum RoundingMode {
  82. /**
  83. * Rounding mode to round away from zero. Always increments the
  84. * digit prior to a non-zero discarded fraction. Note that this
  85. * rounding mode never decreases the magnitude of the calculated
  86. * value.
  87. *
  88. *<p>Example:
  89. *<table border>
  90. *<tr valign=top><th>Input Number</th>
  91. * <th>Input rounded to one digit<br> with <tt>UP</tt> rounding
  92. *<tr align=right><td>5.5</td> <td>6</td>
  93. *<tr align=right><td>2.5</td> <td>3</td>
  94. *<tr align=right><td>1.6</td> <td>2</td>
  95. *<tr align=right><td>1.1</td> <td>2</td>
  96. *<tr align=right><td>1.0</td> <td>1</td>
  97. *<tr align=right><td>-1.0</td> <td>-1</td>
  98. *<tr align=right><td>-1.1</td> <td>-2</td>
  99. *<tr align=right><td>-1.6</td> <td>-2</td>
  100. *<tr align=right><td>-2.5</td> <td>-3</td>
  101. *<tr align=right><td>-5.5</td> <td>-6</td>
  102. *</table>
  103. */
  104. UP(BigDecimal.ROUND_UP),
  105. /**
  106. * Rounding mode to round towards zero. Never increments the digit
  107. * prior to a discarded fraction (i.e., truncates). Note that this
  108. * rounding mode never increases the magnitude of the calculated value.
  109. *
  110. *<p>Example:
  111. *<table border>
  112. *<tr valign=top><th>Input Number</th>
  113. * <th>Input rounded to one digit<br> with <tt>DOWN</tt> rounding
  114. *<tr align=right><td>5.5</td> <td>5</td>
  115. *<tr align=right><td>2.5</td> <td>2</td>
  116. *<tr align=right><td>1.6</td> <td>1</td>
  117. *<tr align=right><td>1.1</td> <td>1</td>
  118. *<tr align=right><td>1.0</td> <td>1</td>
  119. *<tr align=right><td>-1.0</td> <td>-1</td>
  120. *<tr align=right><td>-1.1</td> <td>-1</td>
  121. *<tr align=right><td>-1.6</td> <td>-1</td>
  122. *<tr align=right><td>-2.5</td> <td>-2</td>
  123. *<tr align=right><td>-5.5</td> <td>-5</td>
  124. *</table>
  125. */
  126. DOWN(BigDecimal.ROUND_DOWN),
  127. /**
  128. * Rounding mode to round towards positive infinity. If the
  129. * result is positive, behaves as for <tt>RoundingMode.UP</tt>
  130. * if negative, behaves as for <tt>RoundingMode.DOWN</tt>. Note
  131. * that this rounding mode never decreases the calculated value.
  132. *
  133. *<p>Example:
  134. *<table border>
  135. *<tr valign=top><th>Input Number</th>
  136. * <th>Input rounded to one digit<br> with <tt>CEILING</tt> rounding
  137. *<tr align=right><td>5.5</td> <td>6</td>
  138. *<tr align=right><td>2.5</td> <td>3</td>
  139. *<tr align=right><td>1.6</td> <td>2</td>
  140. *<tr align=right><td>1.1</td> <td>2</td>
  141. *<tr align=right><td>1.0</td> <td>1</td>
  142. *<tr align=right><td>-1.0</td> <td>-1</td>
  143. *<tr align=right><td>-1.1</td> <td>-1</td>
  144. *<tr align=right><td>-1.6</td> <td>-1</td>
  145. *<tr align=right><td>-2.5</td> <td>-2</td>
  146. *<tr align=right><td>-5.5</td> <td>-5</td>
  147. *</table>
  148. */
  149. CEILING(BigDecimal.ROUND_CEILING),
  150. /**
  151. * Rounding mode to round towards negative infinity. If the
  152. * result is positive, behave as for <tt>RoundingMode.DOWN</tt>
  153. * if negative, behave as for <tt>RoundingMode.UP</tt>. Note that
  154. * this rounding mode never increases the calculated value.
  155. *
  156. *<p>Example:
  157. *<table border>
  158. *<tr valign=top><th>Input Number</th>
  159. * <th>Input rounded to one digit<br> with <tt>FLOOR</tt> rounding
  160. *<tr align=right><td>5.5</td> <td>5</td>
  161. *<tr align=right><td>2.5</td> <td>2</td>
  162. *<tr align=right><td>1.6</td> <td>1</td>
  163. *<tr align=right><td>1.1</td> <td>1</td>
  164. *<tr align=right><td>1.0</td> <td>1</td>
  165. *<tr align=right><td>-1.0</td> <td>-1</td>
  166. *<tr align=right><td>-1.1</td> <td>-2</td>
  167. *<tr align=right><td>-1.6</td> <td>-2</td>
  168. *<tr align=right><td>-2.5</td> <td>-3</td>
  169. *<tr align=right><td>-5.5</td> <td>-6</td>
  170. *</table>
  171. */
  172. FLOOR(BigDecimal.ROUND_FLOOR),
  173. /**
  174. * Rounding mode to round towards "nearest neighbor"
  175. * unless both neighbors are equidistant, in which case round up.
  176. * Behaves as for <tt>RoundingMode.UP</tt> if the discarded
  177. * fraction is >= 0.5; otherwise, behaves as for
  178. * <tt>RoundingMode.DOWN</tt>. Note that this is the rounding
  179. * mode commonly taught at school.
  180. *
  181. *<p>Example:
  182. *<table border>
  183. *<tr valign=top><th>Input Number</th>
  184. * <th>Input rounded to one digit<br> with <tt>HALF_UP</tt> rounding
  185. *<tr align=right><td>5.5</td> <td>6</td>
  186. *<tr align=right><td>2.5</td> <td>3</td>
  187. *<tr align=right><td>1.6</td> <td>2</td>
  188. *<tr align=right><td>1.1</td> <td>1</td>
  189. *<tr align=right><td>1.0</td> <td>1</td>
  190. *<tr align=right><td>-1.0</td> <td>-1</td>
  191. *<tr align=right><td>-1.1</td> <td>-1</td>
  192. *<tr align=right><td>-1.6</td> <td>-2</td>
  193. *<tr align=right><td>-2.5</td> <td>-3</td>
  194. *<tr align=right><td>-5.5</td> <td>-6</td>
  195. *</table>
  196. */
  197. HALF_UP(BigDecimal.ROUND_HALF_UP),
  198. /**
  199. * Rounding mode to round towards "nearest neighbor"
  200. * unless both neighbors are equidistant, in which case round
  201. * down. Behaves as for <tt>RoundingMode.UP</tt> if the discarded
  202. * fraction is > 0.5; otherwise, behaves as for
  203. * <tt>RoundingMode.DOWN</tt>.
  204. *
  205. *<p>Example:
  206. *<table border>
  207. *<tr valign=top><th>Input Number</th>
  208. * <th>Input rounded to one digit<br> with <tt>HALF_DOWN</tt> rounding
  209. *<tr align=right><td>5.5</td> <td>5</td>
  210. *<tr align=right><td>2.5</td> <td>2</td>
  211. *<tr align=right><td>1.6</td> <td>2</td>
  212. *<tr align=right><td>1.1</td> <td>1</td>
  213. *<tr align=right><td>1.0</td> <td>1</td>
  214. *<tr align=right><td>-1.0</td> <td>-1</td>
  215. *<tr align=right><td>-1.1</td> <td>-1</td>
  216. *<tr align=right><td>-1.6</td> <td>-2</td>
  217. *<tr align=right><td>-2.5</td> <td>-2</td>
  218. *<tr align=right><td>-5.5</td> <td>-5</td>
  219. *</table>
  220. */
  221. HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
  222. /**
  223. * Rounding mode to round towards the "nearest neighbor"
  224. * unless both neighbors are equidistant, in which case, round
  225. * towards the even neighbor. Behaves as for
  226. * <tt>RoundingMode.HALF_UP</tt> if the digit to the left of the
  227. * discarded fraction is odd; behaves as for
  228. * <tt>RoundingMode.HALF_DOWN</tt> if it's even. Note that this
  229. * is the rounding mode that statistically minimizes cumulative
  230. * error when applied repeatedly over a sequence of calculations.
  231. * It is sometimes known as "Banker's rounding," and is
  232. * chiefly used in the USA. This rounding mode is analogous to
  233. * the rounding policy used for <tt>float</tt> and <tt>double</tt>
  234. * arithmetic in Java.
  235. *
  236. *<p>Example:
  237. *<table border>
  238. *<tr valign=top><th>Input Number</th>
  239. * <th>Input rounded to one digit<br> with <tt>HALF_EVEN</tt> rounding
  240. *<tr align=right><td>5.5</td> <td>6</td>
  241. *<tr align=right><td>2.5</td> <td>2</td>
  242. *<tr align=right><td>1.6</td> <td>2</td>
  243. *<tr align=right><td>1.1</td> <td>1</td>
  244. *<tr align=right><td>1.0</td> <td>1</td>
  245. *<tr align=right><td>-1.0</td> <td>-1</td>
  246. *<tr align=right><td>-1.1</td> <td>-1</td>
  247. *<tr align=right><td>-1.6</td> <td>-2</td>
  248. *<tr align=right><td>-2.5</td> <td>-2</td>
  249. *<tr align=right><td>-5.5</td> <td>-6</td>
  250. *</table>
  251. */
  252. HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
  253. /**
  254. * Rounding mode to assert that the requested operation has an exact
  255. * result, hence no rounding is necessary. If this rounding mode is
  256. * specified on an operation that yields an inexact result, an
  257. * <tt>ArithmeticException</tt> is thrown.
  258. *<p>Example:
  259. *<table border>
  260. *<tr valign=top><th>Input Number</th>
  261. * <th>Input rounded to one digit<br> with <tt>UNNECESSARY</tt> rounding
  262. *<tr align=right><td>5.5</td> <td>throw <tt>ArithmeticException</tt></td>
  263. *<tr align=right><td>2.5</td> <td>throw <tt>ArithmeticException</tt></td>
  264. *<tr align=right><td>1.6</td> <td>throw <tt>ArithmeticException</tt></td>
  265. *<tr align=right><td>1.1</td> <td>throw <tt>ArithmeticException</tt></td>
  266. *<tr align=right><td>1.0</td> <td>1</td>
  267. *<tr align=right><td>-1.0</td> <td>-1</td>
  268. *<tr align=right><td>-1.1</td> <td>throw <tt>ArithmeticException</tt></td>
  269. *<tr align=right><td>-1.6</td> <td>throw <tt>ArithmeticException</tt></td>
  270. *<tr align=right><td>-2.5</td> <td>throw <tt>ArithmeticException</tt></td>
  271. *<tr align=right><td>-5.5</td> <td>throw <tt>ArithmeticException</tt></td>
  272. *</table>
  273. */
  274. UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
  275. // Corresponding BigDecimal rounding constant
  276. final int oldMode;
  277. /**
  278. * Constructor
  279. *
  280. * @param oldMode The <tt>BigDecimal</tt> constant corresponding to
  281. * this mode
  282. */
  283. private RoundingMode(int oldMode) {
  284. this.oldMode = oldMode;
  285. }
  286. /**
  287. * Returns the <tt>RoundingMode</tt> object corresponding to a
  288. * legacy integer rounding mode constant in {@link BigDecimal}.
  289. *
  290. * @param rm legacy integer rounding mode to convert
  291. * @return <tt>RoundingMode</tt> corresponding to the given integer.
  292. * @throws IllegalArgumentException integer is out of range
  293. */
  294. public static RoundingMode valueOf(int rm) {
  295. switch(rm) {
  296. case BigDecimal.ROUND_UP:
  297. return UP;
  298. case BigDecimal.ROUND_DOWN:
  299. return DOWN;
  300. case BigDecimal.ROUND_CEILING:
  301. return CEILING;
  302. case BigDecimal.ROUND_FLOOR:
  303. return FLOOR;
  304. case BigDecimal.ROUND_HALF_UP:
  305. return HALF_UP;
  306. case BigDecimal.ROUND_HALF_DOWN:
  307. return HALF_DOWN;
  308. case BigDecimal.ROUND_HALF_EVEN:
  309. return HALF_EVEN;
  310. case BigDecimal.ROUND_UNNECESSARY:
  311. return UNNECESSARY;
  312. default:
  313. throw new IllegalArgumentException("argument out of range");
  314. }
  315. }
  316. }