public enum RoundingMode extends Enum<RoundingMode>
每一个四舍五入的模式描述包括一个表,列出在四舍五入模式下,不同的两位的两位十进制数会如何圆至一一位的十进制数值。表中的结果列可以通过使用指定的值创建一个BigDecimal
数了,形成了一个具有适当的设置MathContext
对象(precision
设置1
,而设定的舍入模式问题的roundingMode
),并称round
这个号码与适当的MathContext
。显示所有四舍五入模式的这些四舍五入操作的结果的摘要表。
Result of rounding input to one digit with the given rounding mode | ||||||||
---|---|---|---|---|---|---|---|---|
Input Number | UP |
DOWN |
CEILING |
FLOOR |
HALF_UP |
HALF_DOWN |
HALF_EVEN |
UNNECESSARY |
5.5 | 6 | 5 | 6 | 5 | 6 | 5 | 6 | throw ArithmeticException |
2.5 | 3 | 2 | 3 | 2 | 3 | 2 | 2 | throw ArithmeticException |
1.6 | 2 | 1 | 2 | 1 | 2 | 2 | 2 | throw ArithmeticException |
1.1 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | throw ArithmeticException |
1.0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
-1.0 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
-1.1 | -2 | -1 | -1 | -2 | -1 | -1 | -1 | throw ArithmeticException |
-1.6 | -2 | -1 | -1 | -2 | -2 | -2 | -2 | throw ArithmeticException |
-2.5 | -3 | -2 | -2 | -3 | -3 | -2 | -2 | throw ArithmeticException |
-5.5 | -6 | -5 | -5 | -6 | -6 | -5 | -6 | throw ArithmeticException |
这枚举
旨在取代基于整数舍入模式BigDecimal
枚举常数(BigDecimal.ROUND_UP
,BigDecimal.ROUND_DOWN
,等)。
BigDecimal
,
MathContext
Enum Constant and Description |
---|
CEILING
圆向正无穷大的圆整圆。
|
DOWN
圆对零的圆整。
|
FLOOR
圆对负无穷大的四舍五入模式。
|
HALF_DOWN
舍入到“近邻”除非两边距离相等,在这种情况下,本轮下跌。
|
HALF_EVEN
舍入到“近邻”除非两边距离相等,在这种情况下,对连邻居轮。
|
HALF_UP
舍入到“近邻”除非两边距离相等,在这种情况下,圆了。
|
UNNECESSARY
四舍五入模式断言,所请求的操作有一个确切的结果,因此没有四舍五入是必要的。
|
UP
圆距离为零的四舍五入模式。
|
Modifier and Type | Method and Description |
---|---|
static RoundingMode |
valueOf(int rm)
返回一个整数舍入模式的不断
BigDecimal 遗留在相应的
RoundingMode 对象。
|
static RoundingMode |
valueOf(String name)
返回此类型具有指定名称的枚举常量。
|
static RoundingMode[] |
values()
返回一个数组包含该枚举类型的常量,它们的顺序声明。
|
public static final RoundingMode UP
例子:
Input Number | Input rounded to one digit with UP rounding |
---|---|
5.5 | 6 |
2.5 | 3 |
1.6 | 2 |
1.1 | 2 |
1.0 | 1 |
-1.0 | -1 |
-1.1 | -2 |
-1.6 | -2 |
-2.5 | -3 |
-5.5 | -6 |
public static final RoundingMode DOWN
例子:
Input Number | Input rounded to one digit with DOWN rounding |
---|---|
5.5 | 5 |
2.5 | 2 |
1.6 | 1 |
1.1 | 1 |
1.0 | 1 |
-1.0 | -1 |
-1.1 | -1 |
-1.6 | -1 |
-2.5 | -2 |
-5.5 | -5 |
public static final RoundingMode CEILING
RoundingMode.UP
;如果为负,表现为
RoundingMode.DOWN
。请注意,此四舍五入模式从未降低计算出的值。
例子:
Input Number | Input rounded to one digit with CEILING rounding |
---|---|
5.5 | 6 |
2.5 | 3 |
1.6 | 2 |
1.1 | 2 |
1.0 | 1 |
-1.0 | -1 |
-1.1 | -1 |
-1.6 | -1 |
-2.5 | -2 |
-5.5 | -5 |
public static final RoundingMode FLOOR
RoundingMode.DOWN
;如果为负,表现为
RoundingMode.UP
。请注意,此四舍五入模式不会增加计算出的值。
例子:
Input Number | Input rounded to one digit with FLOOR rounding |
---|---|
5.5 | 5 |
2.5 | 2 |
1.6 | 1 |
1.1 | 1 |
1.0 | 1 |
-1.0 | -1 |
-1.1 | -2 |
-1.6 | -2 |
-2.5 | -3 |
-5.5 | -6 |
public static final RoundingMode HALF_UP
RoundingMode.UP
如果丢弃的分数≥0.5;否则,表现为
RoundingMode.DOWN
。请注意,这是在学校常用的四舍五入模式。
例子:
Input Number | Input rounded to one digit with HALF_UP rounding |
---|---|
5.5 | 6 |
2.5 | 3 |
1.6 | 2 |
1.1 | 1 |
1.0 | 1 |
-1.0 | -1 |
-1.1 | -1 |
-1.6 | -2 |
-2.5 | -3 |
-5.5 | -6 |
public static final RoundingMode HALF_DOWN
RoundingMode.UP
如果丢弃率>0.5;否则,表现为
RoundingMode.DOWN
。
例子:
Input Number | Input rounded to one digit with HALF_DOWN rounding |
---|---|
5.5 | 5 |
2.5 | 2 |
1.6 | 2 |
1.1 | 1 |
1.0 | 1 |
-1.0 | -1 |
-1.1 | -1 |
-1.6 | -2 |
-2.5 | -2 |
-5.5 | -5 |
public static final RoundingMode HALF_EVEN
RoundingMode.HALF_UP
如果数字的废弃部分左边是奇数;表现为
RoundingMode.HALF_DOWN
如果它甚至。请注意,这是四舍五入模式,统计最大限度地减少累积误差时,反复应用在一个序列的计算。它有时被称为“银行家舍入,“主要是用在USA.这种舍入模式类似于舍入策略用于java
float
和
double
算法。
例子:
Input Number | Input rounded to one digit with HALF_EVEN rounding |
---|---|
5.5 | 6 |
2.5 | 2 |
1.6 | 2 |
1.1 | 1 |
1.0 | 1 |
-1.0 | -1 |
-1.1 | -1 |
-1.6 | -2 |
-2.5 | -2 |
-5.5 | -6 |
public static final RoundingMode UNNECESSARY
ArithmeticException
抛出。
例子:
Input Number | Input rounded to one digit with UNNECESSARY rounding |
---|---|
5.5 | throw ArithmeticException |
2.5 | throw ArithmeticException |
1.6 | throw ArithmeticException |
1.1 | throw ArithmeticException |
1.0 | 1 |
-1.0 | -1 |
-1.1 | throw ArithmeticException |
-1.6 | throw ArithmeticException |
-2.5 | throw ArithmeticException |
-5.5 | throw ArithmeticException |
public static RoundingMode[] values()
对于(roundingmode C:roundingmode。values()) 系统,println(C);
public static RoundingMode valueOf(String name)
name
-定要返回的枚举的名称。
IllegalArgumentException
-如果这个枚举类型,也没有固定的具有指定名称
NullPointerException
-如果参数为空
public static RoundingMode valueOf(int rm)
BigDecimal
遗留在相应的
RoundingMode
对象。
rm
遗留整数舍入模式转换
RoundingMode
。
IllegalArgumentException
整数超出范围
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.