Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/math/RoundingMode.java
38829 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Portions Copyright IBM Corporation, 2001. All Rights Reserved.27*/28package java.math;2930/**31* Specifies a <i>rounding behavior</i> for numerical operations32* capable of discarding precision. Each rounding mode indicates how33* the least significant returned digit of a rounded result is to be34* calculated. If fewer digits are returned than the digits needed to35* represent the exact numerical result, the discarded digits will be36* referred to as the <i>discarded fraction</i> regardless the digits'37* contribution to the value of the number. In other words,38* considered as a numerical value, the discarded fraction could have39* an absolute value greater than one.40*41* <p>Each rounding mode description includes a table listing how42* different two-digit decimal values would round to a one digit43* decimal value under the rounding mode in question. The result44* column in the tables could be gotten by creating a45* {@code BigDecimal} number with the specified value, forming a46* {@link MathContext} object with the proper settings47* ({@code precision} set to {@code 1}, and the48* {@code roundingMode} set to the rounding mode in question), and49* calling {@link BigDecimal#round round} on this number with the50* proper {@code MathContext}. A summary table showing the results51* of these rounding operations for all rounding modes appears below.52*53*<table border>54* <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption>55* <tr><th></th><th colspan=8>Result of rounding input to one digit with the given56* rounding mode</th>57* <tr valign=top>58* <th>Input Number</th> <th>{@code UP}</th>59* <th>{@code DOWN}</th>60* <th>{@code CEILING}</th>61* <th>{@code FLOOR}</th>62* <th>{@code HALF_UP}</th>63* <th>{@code HALF_DOWN}</th>64* <th>{@code HALF_EVEN}</th>65* <th>{@code UNNECESSARY}</th>66*67* <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 {@code ArithmeticException}</td>68* <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 {@code ArithmeticException}</td>69* <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 {@code ArithmeticException}</td>70* <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 {@code ArithmeticException}</td>71* <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>72* <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>73* <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 {@code ArithmeticException}</td>74* <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 {@code ArithmeticException}</td>75* <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 {@code ArithmeticException}</td>76* <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 {@code ArithmeticException}</td>77*</table>78*79*80* <p>This {@code enum} is intended to replace the integer-based81* enumeration of rounding mode constants in {@link BigDecimal}82* ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},83* etc. ).84*85* @see BigDecimal86* @see MathContext87* @author Josh Bloch88* @author Mike Cowlishaw89* @author Joseph D. Darcy90* @since 1.591*/92public enum RoundingMode {9394/**95* Rounding mode to round away from zero. Always increments the96* digit prior to a non-zero discarded fraction. Note that this97* rounding mode never decreases the magnitude of the calculated98* value.99*100*<p>Example:101*<table border>102* <caption><b>Rounding mode UP Examples</b></caption>103*<tr valign=top><th>Input Number</th>104* <th>Input rounded to one digit<br> with {@code UP} rounding105*<tr align=right><td>5.5</td> <td>6</td>106*<tr align=right><td>2.5</td> <td>3</td>107*<tr align=right><td>1.6</td> <td>2</td>108*<tr align=right><td>1.1</td> <td>2</td>109*<tr align=right><td>1.0</td> <td>1</td>110*<tr align=right><td>-1.0</td> <td>-1</td>111*<tr align=right><td>-1.1</td> <td>-2</td>112*<tr align=right><td>-1.6</td> <td>-2</td>113*<tr align=right><td>-2.5</td> <td>-3</td>114*<tr align=right><td>-5.5</td> <td>-6</td>115*</table>116*/117UP(BigDecimal.ROUND_UP),118119/**120* Rounding mode to round towards zero. Never increments the digit121* prior to a discarded fraction (i.e., truncates). Note that this122* rounding mode never increases the magnitude of the calculated value.123*124*<p>Example:125*<table border>126* <caption><b>Rounding mode DOWN Examples</b></caption>127*<tr valign=top><th>Input Number</th>128* <th>Input rounded to one digit<br> with {@code DOWN} rounding129*<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*/141DOWN(BigDecimal.ROUND_DOWN),142143/**144* Rounding mode to round towards positive infinity. If the145* result is positive, behaves as for {@code RoundingMode.UP};146* if negative, behaves as for {@code RoundingMode.DOWN}. Note147* that this rounding mode never decreases the calculated value.148*149*<p>Example:150*<table border>151* <caption><b>Rounding mode CEILING Examples</b></caption>152*<tr valign=top><th>Input Number</th>153* <th>Input rounded to one digit<br> with {@code CEILING} rounding154*<tr align=right><td>5.5</td> <td>6</td>155*<tr align=right><td>2.5</td> <td>3</td>156*<tr align=right><td>1.6</td> <td>2</td>157*<tr align=right><td>1.1</td> <td>2</td>158*<tr align=right><td>1.0</td> <td>1</td>159*<tr align=right><td>-1.0</td> <td>-1</td>160*<tr align=right><td>-1.1</td> <td>-1</td>161*<tr align=right><td>-1.6</td> <td>-1</td>162*<tr align=right><td>-2.5</td> <td>-2</td>163*<tr align=right><td>-5.5</td> <td>-5</td>164*</table>165*/166CEILING(BigDecimal.ROUND_CEILING),167168/**169* Rounding mode to round towards negative infinity. If the170* result is positive, behave as for {@code RoundingMode.DOWN};171* if negative, behave as for {@code RoundingMode.UP}. Note that172* this rounding mode never increases the calculated value.173*174*<p>Example:175*<table border>176* <caption><b>Rounding mode FLOOR Examples</b></caption>177*<tr valign=top><th>Input Number</th>178* <th>Input rounded to one digit<br> with {@code FLOOR} rounding179*<tr align=right><td>5.5</td> <td>5</td>180*<tr align=right><td>2.5</td> <td>2</td>181*<tr align=right><td>1.6</td> <td>1</td>182*<tr align=right><td>1.1</td> <td>1</td>183*<tr align=right><td>1.0</td> <td>1</td>184*<tr align=right><td>-1.0</td> <td>-1</td>185*<tr align=right><td>-1.1</td> <td>-2</td>186*<tr align=right><td>-1.6</td> <td>-2</td>187*<tr align=right><td>-2.5</td> <td>-3</td>188*<tr align=right><td>-5.5</td> <td>-6</td>189*</table>190*/191FLOOR(BigDecimal.ROUND_FLOOR),192193/**194* Rounding mode to round towards {@literal "nearest neighbor"}195* unless both neighbors are equidistant, in which case round up.196* Behaves as for {@code RoundingMode.UP} if the discarded197* fraction is ≥ 0.5; otherwise, behaves as for198* {@code RoundingMode.DOWN}. Note that this is the rounding199* mode commonly taught at school.200*201*<p>Example:202*<table border>203* <caption><b>Rounding mode HALF_UP Examples</b></caption>204*<tr valign=top><th>Input Number</th>205* <th>Input rounded to one digit<br> with {@code HALF_UP} rounding206*<tr align=right><td>5.5</td> <td>6</td>207*<tr align=right><td>2.5</td> <td>3</td>208*<tr align=right><td>1.6</td> <td>2</td>209*<tr align=right><td>1.1</td> <td>1</td>210*<tr align=right><td>1.0</td> <td>1</td>211*<tr align=right><td>-1.0</td> <td>-1</td>212*<tr align=right><td>-1.1</td> <td>-1</td>213*<tr align=right><td>-1.6</td> <td>-2</td>214*<tr align=right><td>-2.5</td> <td>-3</td>215*<tr align=right><td>-5.5</td> <td>-6</td>216*</table>217*/218HALF_UP(BigDecimal.ROUND_HALF_UP),219220/**221* Rounding mode to round towards {@literal "nearest neighbor"}222* unless both neighbors are equidistant, in which case round223* down. Behaves as for {@code RoundingMode.UP} if the discarded224* fraction is > 0.5; otherwise, behaves as for225* {@code RoundingMode.DOWN}.226*227*<p>Example:228*<table border>229* <caption><b>Rounding mode HALF_DOWN Examples</b></caption>230*<tr valign=top><th>Input Number</th>231* <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding232*<tr align=right><td>5.5</td> <td>5</td>233*<tr align=right><td>2.5</td> <td>2</td>234*<tr align=right><td>1.6</td> <td>2</td>235*<tr align=right><td>1.1</td> <td>1</td>236*<tr align=right><td>1.0</td> <td>1</td>237*<tr align=right><td>-1.0</td> <td>-1</td>238*<tr align=right><td>-1.1</td> <td>-1</td>239*<tr align=right><td>-1.6</td> <td>-2</td>240*<tr align=right><td>-2.5</td> <td>-2</td>241*<tr align=right><td>-5.5</td> <td>-5</td>242*</table>243*/244HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),245246/**247* Rounding mode to round towards the {@literal "nearest neighbor"}248* unless both neighbors are equidistant, in which case, round249* towards the even neighbor. Behaves as for250* {@code RoundingMode.HALF_UP} if the digit to the left of the251* discarded fraction is odd; behaves as for252* {@code RoundingMode.HALF_DOWN} if it's even. Note that this253* is the rounding mode that statistically minimizes cumulative254* error when applied repeatedly over a sequence of calculations.255* It is sometimes known as {@literal "Banker's rounding,"} and is256* chiefly used in the USA. This rounding mode is analogous to257* the rounding policy used for {@code float} and {@code double}258* arithmetic in Java.259*260*<p>Example:261*<table border>262* <caption><b>Rounding mode HALF_EVEN Examples</b></caption>263*<tr valign=top><th>Input Number</th>264* <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding265*<tr align=right><td>5.5</td> <td>6</td>266*<tr align=right><td>2.5</td> <td>2</td>267*<tr align=right><td>1.6</td> <td>2</td>268*<tr align=right><td>1.1</td> <td>1</td>269*<tr align=right><td>1.0</td> <td>1</td>270*<tr align=right><td>-1.0</td> <td>-1</td>271*<tr align=right><td>-1.1</td> <td>-1</td>272*<tr align=right><td>-1.6</td> <td>-2</td>273*<tr align=right><td>-2.5</td> <td>-2</td>274*<tr align=right><td>-5.5</td> <td>-6</td>275*</table>276*/277HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),278279/**280* Rounding mode to assert that the requested operation has an exact281* result, hence no rounding is necessary. If this rounding mode is282* specified on an operation that yields an inexact result, an283* {@code ArithmeticException} is thrown.284*<p>Example:285*<table border>286* <caption><b>Rounding mode UNNECESSARY Examples</b></caption>287*<tr valign=top><th>Input Number</th>288* <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding289*<tr align=right><td>5.5</td> <td>throw {@code ArithmeticException}</td>290*<tr align=right><td>2.5</td> <td>throw {@code ArithmeticException}</td>291*<tr align=right><td>1.6</td> <td>throw {@code ArithmeticException}</td>292*<tr align=right><td>1.1</td> <td>throw {@code ArithmeticException}</td>293*<tr align=right><td>1.0</td> <td>1</td>294*<tr align=right><td>-1.0</td> <td>-1</td>295*<tr align=right><td>-1.1</td> <td>throw {@code ArithmeticException}</td>296*<tr align=right><td>-1.6</td> <td>throw {@code ArithmeticException}</td>297*<tr align=right><td>-2.5</td> <td>throw {@code ArithmeticException}</td>298*<tr align=right><td>-5.5</td> <td>throw {@code ArithmeticException}</td>299*</table>300*/301UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);302303// Corresponding BigDecimal rounding constant304final int oldMode;305306/**307* Constructor308*309* @param oldMode The {@code BigDecimal} constant corresponding to310* this mode311*/312private RoundingMode(int oldMode) {313this.oldMode = oldMode;314}315316/**317* Returns the {@code RoundingMode} object corresponding to a318* legacy integer rounding mode constant in {@link BigDecimal}.319*320* @param rm legacy integer rounding mode to convert321* @return {@code RoundingMode} corresponding to the given integer.322* @throws IllegalArgumentException integer is out of range323*/324public static RoundingMode valueOf(int rm) {325switch(rm) {326327case BigDecimal.ROUND_UP:328return UP;329330case BigDecimal.ROUND_DOWN:331return DOWN;332333case BigDecimal.ROUND_CEILING:334return CEILING;335336case BigDecimal.ROUND_FLOOR:337return FLOOR;338339case BigDecimal.ROUND_HALF_UP:340return HALF_UP;341342case BigDecimal.ROUND_HALF_DOWN:343return HALF_DOWN;344345case BigDecimal.ROUND_HALF_EVEN:346return HALF_EVEN;347348case BigDecimal.ROUND_UNNECESSARY:349return UNNECESSARY;350351default:352throw new IllegalArgumentException("argument out of range");353}354}355}356357358