Path: blob/master/src/java.base/share/classes/java/math/RoundingMode.java
41152 views
/*1* Copyright (c) 2003, 2021, 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 policy</i> for numerical operations capable32* of discarding precision. Each rounding mode indicates how the least33* significant returned digit of a rounded result is to be calculated.34* If fewer digits are returned than the digits needed to represent35* the exact numerical result, the discarded digits will be referred36* 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 class="striped">54* <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption>55* <thead>56* <tr><th scope="col" rowspan="2">Input Number</th><th scope="col"colspan=8>Result of rounding input to one digit with the given57* rounding mode</th>58* <tr style="vertical-align:top">59* <th>{@code UP}</th>60* <th>{@code DOWN}</th>61* <th>{@code CEILING}</th>62* <th>{@code FLOOR}</th>63* <th>{@code HALF_UP}</th>64* <th>{@code HALF_DOWN}</th>65* <th>{@code HALF_EVEN}</th>66* <th>{@code UNNECESSARY}</th>67* </thead>68* <tbody style="text-align:right">69*70* <tr><th scope="row">5.5</th> <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>71* <tr><th scope="row">2.5</th> <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>72* <tr><th scope="row">1.6</th> <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>73* <tr><th scope="row">1.1</th> <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>74* <tr><th scope="row">1.0</th> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td>75* <tr><th scope="row">-1.0</th> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td> <td>-1</td>76* <tr><th scope="row">-1.1</th> <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>77* <tr><th scope="row">-1.6</th> <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>78* <tr><th scope="row">-2.5</th> <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>79* <tr><th scope="row">-5.5</th> <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>80* </tbody>81* </table>82*83*84* <p>This {@code enum} is intended to replace the integer-based85* enumeration of rounding mode constants in {@link BigDecimal}86* ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},87* etc. ).88*89* @apiNote90* Five of the rounding modes declared in this class correspond to91* rounding-direction attributes defined in the <cite>IEEE Standard92* for Floating-Point Arithmetic</cite>, IEEE 754-2019. Where present,93* this correspondence will be noted in the documentation of the94* particular constant.95*96* @see BigDecimal97* @see MathContext98* @author Josh Bloch99* @author Mike Cowlishaw100* @author Joseph D. Darcy101* @since 1.5102*/103@SuppressWarnings("deprecation") // Legacy rounding mode constants in BigDecimal104public enum RoundingMode {105106/**107* Rounding mode to round away from zero. Always increments the108* digit prior to a non-zero discarded fraction. Note that this109* rounding mode never decreases the magnitude of the calculated110* value.111*112*<p>Example:113*<table class="striped">114* <caption>Rounding mode UP Examples</caption>115*<thead>116*<tr style="vertical-align:top"><th scope="col">Input Number</th>117* <th scope="col">Input rounded to one digit<br> with {@code UP} rounding118*</thead>119*<tbody style="text-align:right">120*<tr><th scope="row">5.5</th> <td>6</td>121*<tr><th scope="row">2.5</th> <td>3</td>122*<tr><th scope="row">1.6</th> <td>2</td>123*<tr><th scope="row">1.1</th> <td>2</td>124*<tr><th scope="row">1.0</th> <td>1</td>125*<tr><th scope="row">-1.0</th> <td>-1</td>126*<tr><th scope="row">-1.1</th> <td>-2</td>127*<tr><th scope="row">-1.6</th> <td>-2</td>128*<tr><th scope="row">-2.5</th> <td>-3</td>129*<tr><th scope="row">-5.5</th> <td>-6</td>130*</tbody>131*</table>132*/133UP(BigDecimal.ROUND_UP),134135/**136* Rounding mode to round towards zero. Never increments the digit137* prior to a discarded fraction (i.e., truncates). Note that this138* rounding mode never increases the magnitude of the calculated value.139* This mode corresponds to the IEEE 754-2019 rounding-direction140* attribute roundTowardZero.141*142*<p>Example:143*<table class="striped">144* <caption>Rounding mode DOWN Examples</caption>145*<thead>146*<tr style="vertical-align:top"><th scope="col">Input Number</th>147* <th scope="col">Input rounded to one digit<br> with {@code DOWN} rounding148*</thead>149*<tbody style="text-align:right">150*<tr><th scope="row">5.5</th> <td>5</td>151*<tr><th scope="row">2.5</th> <td>2</td>152*<tr><th scope="row">1.6</th> <td>1</td>153*<tr><th scope="row">1.1</th> <td>1</td>154*<tr><th scope="row">1.0</th> <td>1</td>155*<tr><th scope="row">-1.0</th> <td>-1</td>156*<tr><th scope="row">-1.1</th> <td>-1</td>157*<tr><th scope="row">-1.6</th> <td>-1</td>158*<tr><th scope="row">-2.5</th> <td>-2</td>159*<tr><th scope="row">-5.5</th> <td>-5</td>160*</tbody>161*</table>162*/163DOWN(BigDecimal.ROUND_DOWN),164165/**166* Rounding mode to round towards positive infinity. If the167* result is positive, behaves as for {@code RoundingMode.UP};168* if negative, behaves as for {@code RoundingMode.DOWN}. Note169* that this rounding mode never decreases the calculated value.170* This mode corresponds to the IEEE 754-2019 rounding-direction171* attribute roundTowardPositive.172*173*<p>Example:174*<table class="striped">175* <caption>Rounding mode CEILING Examples</caption>176*<thead>177*<tr style="vertical-align:top"><th>Input Number</th>178* <th>Input rounded to one digit<br> with {@code CEILING} rounding179*</thead>180*<tbody style="text-align:right">181*<tr><th scope="row">5.5</th> <td>6</td>182*<tr><th scope="row">2.5</th> <td>3</td>183*<tr><th scope="row">1.6</th> <td>2</td>184*<tr><th scope="row">1.1</th> <td>2</td>185*<tr><th scope="row">1.0</th> <td>1</td>186*<tr><th scope="row">-1.0</th> <td>-1</td>187*<tr><th scope="row">-1.1</th> <td>-1</td>188*<tr><th scope="row">-1.6</th> <td>-1</td>189*<tr><th scope="row">-2.5</th> <td>-2</td>190*<tr><th scope="row">-5.5</th> <td>-5</td>191*</tbody>192*</table>193*/194CEILING(BigDecimal.ROUND_CEILING),195196/**197* Rounding mode to round towards negative infinity. If the198* result is positive, behave as for {@code RoundingMode.DOWN};199* if negative, behave as for {@code RoundingMode.UP}. Note that200* this rounding mode never increases the calculated value.201* This mode corresponds to the IEEE 754-2019 rounding-direction202* attribute roundTowardNegative.203*204*<p>Example:205*<table class="striped">206* <caption>Rounding mode FLOOR Examples</caption>207*<thead>208*<tr style="vertical-align:top"><th scope="col">Input Number</th>209* <th scope="col">Input rounded to one digit<br> with {@code FLOOR} rounding210*</thead>211*<tbody style="text-align:right">212*<tr><th scope="row">5.5</th> <td>5</td>213*<tr><th scope="row">2.5</th> <td>2</td>214*<tr><th scope="row">1.6</th> <td>1</td>215*<tr><th scope="row">1.1</th> <td>1</td>216*<tr><th scope="row">1.0</th> <td>1</td>217*<tr><th scope="row">-1.0</th> <td>-1</td>218*<tr><th scope="row">-1.1</th> <td>-2</td>219*<tr><th scope="row">-1.6</th> <td>-2</td>220*<tr><th scope="row">-2.5</th> <td>-3</td>221*<tr><th scope="row">-5.5</th> <td>-6</td>222*</tbody>223*</table>224*/225FLOOR(BigDecimal.ROUND_FLOOR),226227/**228* Rounding mode to round towards {@literal "nearest neighbor"}229* unless both neighbors are equidistant, in which case round up.230* Behaves as for {@code RoundingMode.UP} if the discarded231* fraction is ≥ 0.5; otherwise, behaves as for232* {@code RoundingMode.DOWN}. Note that this is the rounding233* mode commonly taught at school.234* This mode corresponds to the IEEE 754-2019 rounding-direction235* attribute roundTiesToAway.236*237*<p>Example:238*<table class="striped">239* <caption>Rounding mode HALF_UP Examples</caption>240*<thead>241*<tr style="vertical-align:top"><th scope="col">Input Number</th>242* <th scope="col">Input rounded to one digit<br> with {@code HALF_UP} rounding243*</thead>244*<tbody style="text-align:right">245*<tr><th scope="row">5.5</th> <td>6</td>246*<tr><th scope="row">2.5</th> <td>3</td>247*<tr><th scope="row">1.6</th> <td>2</td>248*<tr><th scope="row">1.1</th> <td>1</td>249*<tr><th scope="row">1.0</th> <td>1</td>250*<tr><th scope="row">-1.0</th> <td>-1</td>251*<tr><th scope="row">-1.1</th> <td>-1</td>252*<tr><th scope="row">-1.6</th> <td>-2</td>253*<tr><th scope="row">-2.5</th> <td>-3</td>254*<tr><th scope="row">-5.5</th> <td>-6</td>255*</tbody>256*</table>257*/258HALF_UP(BigDecimal.ROUND_HALF_UP),259260/**261* Rounding mode to round towards {@literal "nearest neighbor"}262* unless both neighbors are equidistant, in which case round263* down. Behaves as for {@code RoundingMode.UP} if the discarded264* fraction is > 0.5; otherwise, behaves as for265* {@code RoundingMode.DOWN}.266*267*<p>Example:268*<table class="striped">269* <caption>Rounding mode HALF_DOWN Examples</caption>270*<thead>271*<tr style="vertical-align:top"><th scope="col">Input Number</th>272* <th scope="col">Input rounded to one digit<br> with {@code HALF_DOWN} rounding273*</thead>274*<tbody style="text-align:right">275*<tr><th scope="row">5.5</th> <td>5</td>276*<tr><th scope="row">2.5</th> <td>2</td>277*<tr><th scope="row">1.6</th> <td>2</td>278*<tr><th scope="row">1.1</th> <td>1</td>279*<tr><th scope="row">1.0</th> <td>1</td>280*<tr><th scope="row">-1.0</th> <td>-1</td>281*<tr><th scope="row">-1.1</th> <td>-1</td>282*<tr><th scope="row">-1.6</th> <td>-2</td>283*<tr><th scope="row">-2.5</th> <td>-2</td>284*<tr><th scope="row">-5.5</th> <td>-5</td>285*</tbody>286*</table>287*/288HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),289290/**291* Rounding mode to round towards the {@literal "nearest neighbor"}292* unless both neighbors are equidistant, in which case, round293* towards the even neighbor. Behaves as for294* {@code RoundingMode.HALF_UP} if the digit to the left of the295* discarded fraction is odd; behaves as for296* {@code RoundingMode.HALF_DOWN} if it's even. Note that this297* is the rounding mode that statistically minimizes cumulative298* error when applied repeatedly over a sequence of calculations.299* It is sometimes known as {@literal "Banker's rounding,"} and is300* chiefly used in the USA. This rounding mode is analogous to301* the rounding policy used for {@code float} and {@code double}302* arithmetic in Java.303* This mode corresponds to the IEEE 754-2019 rounding-direction304* attribute roundTiesToEven.305*306*<p>Example:307*<table class="striped">308* <caption>Rounding mode HALF_EVEN Examples</caption>309*<thead>310*<tr style="vertical-align:top"><th scope="col">Input Number</th>311* <th scope="col">Input rounded to one digit<br> with {@code HALF_EVEN} rounding312*</thead>313*<tbody style="text-align:right">314*<tr><th scope="row">5.5</th> <td>6</td>315*<tr><th scope="row">2.5</th> <td>2</td>316*<tr><th scope="row">1.6</th> <td>2</td>317*<tr><th scope="row">1.1</th> <td>1</td>318*<tr><th scope="row">1.0</th> <td>1</td>319*<tr><th scope="row">-1.0</th> <td>-1</td>320*<tr><th scope="row">-1.1</th> <td>-1</td>321*<tr><th scope="row">-1.6</th> <td>-2</td>322*<tr><th scope="row">-2.5</th> <td>-2</td>323*<tr><th scope="row">-5.5</th> <td>-6</td>324*</tbody>325*</table>326*/327HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),328329/**330* Rounding mode to assert that the requested operation has an exact331* result, hence no rounding is necessary. If this rounding mode is332* specified on an operation that yields an inexact result, an333* {@code ArithmeticException} is thrown.334*<p>Example:335*<table class="striped">336* <caption>Rounding mode UNNECESSARY Examples</caption>337*<thead>338*<tr style="vertical-align:top"><th scope="col">Input Number</th>339* <th scope="col">Input rounded to one digit<br> with {@code UNNECESSARY} rounding340*</thead>341*<tbody style="text-align:right">342*<tr><th scope="row">5.5</th> <td>throw {@code ArithmeticException}</td>343*<tr><th scope="row">2.5</th> <td>throw {@code ArithmeticException}</td>344*<tr><th scope="row">1.6</th> <td>throw {@code ArithmeticException}</td>345*<tr><th scope="row">1.1</th> <td>throw {@code ArithmeticException}</td>346*<tr><th scope="row">1.0</th> <td>1</td>347*<tr><th scope="row">-1.0</th> <td>-1</td>348*<tr><th scope="row">-1.1</th> <td>throw {@code ArithmeticException}</td>349*<tr><th scope="row">-1.6</th> <td>throw {@code ArithmeticException}</td>350*<tr><th scope="row">-2.5</th> <td>throw {@code ArithmeticException}</td>351*<tr><th scope="row">-5.5</th> <td>throw {@code ArithmeticException}</td>352*</tbody>353*</table>354*/355UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);356357// Corresponding BigDecimal rounding constant358final int oldMode;359360/**361* Constructor362*363* @param oldMode The {@code BigDecimal} constant corresponding to364* this mode365*/366private RoundingMode(int oldMode) {367this.oldMode = oldMode;368}369370/**371* Returns the {@code RoundingMode} object corresponding to a372* legacy integer rounding mode constant in {@link BigDecimal}.373*374* @param rm legacy integer rounding mode to convert375* @return {@code RoundingMode} corresponding to the given integer.376* @throws IllegalArgumentException integer is out of range377*/378public static RoundingMode valueOf(int rm) {379return switch (rm) {380case BigDecimal.ROUND_UP -> UP;381case BigDecimal.ROUND_DOWN -> DOWN;382case BigDecimal.ROUND_CEILING -> CEILING;383case BigDecimal.ROUND_FLOOR -> FLOOR;384case BigDecimal.ROUND_HALF_UP -> HALF_UP;385case BigDecimal.ROUND_HALF_DOWN -> HALF_DOWN;386case BigDecimal.ROUND_HALF_EVEN -> HALF_EVEN;387case BigDecimal.ROUND_UNNECESSARY -> UNNECESSARY;388default -> throw new IllegalArgumentException("argument out of range");389};390}391}392393394