1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.math; 19 20 /** 21 * Specifies the rounding behavior for operations whose results cannot be 22 * represented exactly. 23 */ 24 public enum RoundingMode { 25 26 /** 27 * Rounding mode where positive values are rounded towards positive infinity 28 * and negative values towards negative infinity. 29 * <br> 30 * Rule: {@code x.round().abs() >= x.abs()} 31 */ 32 UP(BigDecimal.ROUND_UP), 33 34 /** 35 * Rounding mode where the values are rounded towards zero. 36 * <br> 37 * Rule: {@code x.round().abs() <= x.abs()} 38 */ 39 DOWN(BigDecimal.ROUND_DOWN), 40 41 /** 42 * Rounding mode to round towards positive infinity. For positive values 43 * this rounding mode behaves as {@link #UP}, for negative values as 44 * {@link #DOWN}. 45 * <br> 46 * Rule: {@code x.round() >= x} 47 */ 48 CEILING(BigDecimal.ROUND_CEILING), 49 50 /** 51 * Rounding mode to round towards negative infinity. For positive values 52 * this rounding mode behaves as {@link #DOWN}, for negative values as 53 * {@link #UP}. 54 * <br> 55 * Rule: {@code x.round() <= x} 56 */ 57 FLOOR(BigDecimal.ROUND_FLOOR), 58 59 /** 60 * Rounding mode where values are rounded towards the nearest neighbor. Ties 61 * are broken by rounding up. 62 */ 63 HALF_UP(BigDecimal.ROUND_HALF_UP), 64 65 /** 66 * Rounding mode where values are rounded towards the nearest neighbor. Ties 67 * are broken by rounding down. 68 */ 69 HALF_DOWN(BigDecimal.ROUND_HALF_DOWN), 70 71 /** 72 * Rounding mode where values are rounded towards the nearest neighbor. Ties 73 * are broken by rounding to the even neighbor. 74 */ 75 HALF_EVEN(BigDecimal.ROUND_HALF_EVEN), 76 77 /** 78 * Rounding mode where the rounding operations throws an ArithmeticException 79 * for the case that rounding is necessary, i.e. for the case that the value 80 * cannot be represented exactly. 81 */ 82 UNNECESSARY(BigDecimal.ROUND_UNNECESSARY); 83 84 /** The old constant of <code>BigDecimal</code>. */ 85 private final int bigDecimalRM; 86 87 /** It sets the old constant. */ RoundingMode(int rm)88 RoundingMode(int rm) { 89 bigDecimalRM = rm; 90 } 91 92 /** 93 * Converts rounding mode constants from class {@code BigDecimal} into 94 * {@code RoundingMode} values. 95 * 96 * @param mode 97 * rounding mode constant as defined in class {@code BigDecimal} 98 * @return corresponding rounding mode object 99 */ valueOf(int mode)100 public static RoundingMode valueOf(int mode) { 101 switch (mode) { 102 case BigDecimal.ROUND_CEILING: 103 return CEILING; 104 case BigDecimal.ROUND_DOWN: 105 return DOWN; 106 case BigDecimal.ROUND_FLOOR: 107 return FLOOR; 108 case BigDecimal.ROUND_HALF_DOWN: 109 return HALF_DOWN; 110 case BigDecimal.ROUND_HALF_EVEN: 111 return HALF_EVEN; 112 case BigDecimal.ROUND_HALF_UP: 113 return HALF_UP; 114 case BigDecimal.ROUND_UNNECESSARY: 115 return UNNECESSARY; 116 case BigDecimal.ROUND_UP: 117 return UP; 118 default: 119 throw new IllegalArgumentException("Invalid rounding mode"); 120 } 121 } 122 } 123