public enum RoundingMode extends Enum<RoundingMode>
每个舍入模式描述包括一个表,列出了在所讨论的四舍五入模式下,不同的两位十进制值将如何舍入一位数十进制值。 表中的结果列可通过创建一个能够得到BigDecimal数与指定值,形成一个MathContext与适当的设置对象( precision设置为1 ,和roundingMode组到所讨论的舍入模式),并调用round在此数字与正确的MathContext 。 显示所有舍入模式的这些舍入操作的结果的汇总表如下所示。
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)
返回 RoundingMode对应于遗留整数舍入模式恒定对象BigDecimal 。
|
static RoundingMode |
valueOf(String name)
以指定的名称返回此类型的枚举常量。
|
static RoundingMode[] |
values()
按照它们声明的顺序返回一个包含此枚举类型常量的数组。
|
public static final RoundingMode UP
例:
Rounding mode UP Examples Input Number Input rounded to one digitUP 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
例:
Rounding mode DOWN Examples Input Number Input rounded to one digitDOWN 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 。
请注意,舍入模式不会降低计算值。
例:
Rounding mode CEILING Examples Input Number Input rounded to one digitCEILING 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 。
请注意,舍入模式不会增加计算值。
例:
Rounding mode FLOOR Examples Input Number Input rounded to one digitFLOOR 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 。
请注意,这是通常在学校教授的舍入模式。
例:
Rounding mode HALF_UP Examples Input Number Input rounded to one digitHALF_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 。
例:
Rounding mode HALF_DOWN Examples Input Number Input rounded to one digitHALF_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如果它是RoundingMode.HALF_DOWN行为。
请注意,这是舍入模式,统计最小化在一系列计算中重复应用时的累积误差。
有时被称为“银行家四舍五入”,主要用于美国。
这种舍入模式类似于Java中用于float和double算术的舍入策略。
例:
Rounding mode HALF_EVEN Examples Input Number Input rounded to one digitHALF_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 。
例:
Rounding mode UNNECESSARY Examples Input Number Input rounded to one digitUNNECESSARY 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()
for (RoundingMode c : RoundingMode.values())
System.out.println(c);
public static RoundingMode valueOf(String name)
name - 要返回的枚举常量的名称。
IllegalArgumentException - 如果此枚举类型没有指定名称的常量
NullPointerException - 如果参数为空
public static RoundingMode valueOf(int rm)
RoundingMode对应于BigDecimal中遗留整数舍入模式常量的对象 。
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.