- java.lang.Object
-
- java.math.MathContext
-
- All Implemented Interfaces:
-
Serializable
public final class MathContext extends Object implements Serializable
封装描述数值运算符某些规则的上下文设置的不可变对象,例如由BigDecimal类实现的那些规则。独立于基地的设置有:
-
precision:用于操作的位数; 结果四舍五入到这个精度 -
roundingMode:一个RoundingMode对象,其指定用于舍入的算法。
- 从以下版本开始:
- 1.5
- 另请参见:
-
BigDecimal,RoundingMode, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field 描述 static MathContextDECIMAL128AMathContext对象,其精度设置与IEEE 754R Decimal128格式,34位数字以及HALF_EVEN的舍入模式(IEEE 754R默认值)匹配。static MathContextDECIMAL32AMathContext对象,其精度设置与IEEE 754R Decimal32格式,7位数字以及HALF_EVEN的舍入模式(IEEE 754R默认值)匹配。static MathContextDECIMAL64AMathContext对象,其精度设置与IEEE 754R Decimal64格式,16位数字以及HALF_EVEN的舍入模式(IEEE 754R默认值)相匹配。static MathContextUNLIMITED一个MathContext对象,其设置具有无限精度算术所需的值。
-
构造方法摘要
构造方法 Constructor 描述 MathContext(int setPrecision)构造新的MathContext具有指定的精度和HALF_UP舍入模式。MathContext(int setPrecision, RoundingMode setRoundingMode)构造新的MathContext具有指定的精度和舍入模式。MathContext(String val)从一个字符串构造一个新的MathContext。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 booleanequals(Object x)将此MathContext与指定的Object进行比较以获得相等性。intgetPrecision()返回precision设置。RoundingModegetRoundingMode()返回roundingMode设置。inthashCode()返回此MathContext的哈希码。StringtoString()返回此MathContext的字符串表示MathContext。
-
-
-
字段详细信息
-
UNLIMITED
public static final MathContext UNLIMITED
一个MathContext对象,其设置具有无限精度算术所需的值。 设置的值为:precision=0 roundingMode=HALF_UP
-
DECIMAL32
public static final MathContext DECIMAL32
AMathContext对象,具有匹配IEEE 754R Decimal32格式,7位数的精度设置和HALF_EVEN的舍入模式,IEEE 754R默认值。
-
DECIMAL64
public static final MathContext DECIMAL64
AMathContext对象,其精度设置与IEEE 754R Decimal64格式,16位数字以及HALF_EVEN的舍入模式(IEEE 754R默认值)匹配。
-
DECIMAL128
public static final MathContext DECIMAL128
AMathContext对象,其精度设置与IEEE 754R Decimal128格式,34位数字以及HALF_EVEN的舍入模式(IEEE 754R默认值)匹配。
-
-
构造方法详细信息
-
MathContext
public MathContext(int setPrecision)
构造新的MathContext具有指定的精度和HALF_UP舍入模式。- 参数
-
setPrecision- 非负int精度设置。 - 异常
-
IllegalArgumentException- 如果setPrecision参数小于零。
-
MathContext
public MathContext(int setPrecision, RoundingMode setRoundingMode)构造新的MathContext,具有指定的精度和舍入模式。- 参数
-
setPrecision- 非负int精度设置。 -
setRoundingMode- 使用的舍入模式。 - 异常
-
IllegalArgumentException- 如果setPrecision参数小于零。 -
NullPointerException- 如果舍入模式参数为null
-
MathContext
public MathContext(String val)
从一个字符串构造一个新的MathContext。 字符串必须与toString()方法所生成的格式相同。一个
IllegalArgumentException抛出如果字符串的精度部分超出范围(< 0)或字符串不是在由创建的格式toString()方法。- 参数
-
val- 要解析的字符串 - 异常
-
IllegalArgumentException- 如果精度部分超出范围或格式不正确 -
NullPointerException- 如果参数是null
-
-
方法详细信息
-
getPrecision
public int getPrecision()
返回precision设置。 该值始终为非负数。- 结果
-
一个
int这是precision设置的值
-
getRoundingMode
public RoundingMode getRoundingMode()
返回roundingMode设置。 这将是一个RoundingMode.CEILING,RoundingMode.DOWN,RoundingMode.FLOOR,RoundingMode.HALF_DOWN,RoundingMode.HALF_EVEN,RoundingMode.HALF_UP,RoundingMode.UNNECESSARY,或RoundingMode.UP。- 结果
-
一个
RoundingMode对象这是的值roundingMode设置
-
equals
public boolean equals(Object x)
将此MathContext与指定的Object进行比较,以获得相等性。- 重写:
-
equals在Object - 参数
-
x-Object要比较这个MathContext。 - 结果
-
true如果且仅当指定的Object是具有与此对象完全相同设置的MathContext对象 - 另请参见:
-
Object.hashCode(),HashMap
-
hashCode
public int hashCode()
返回此MathContext的哈希码。- 重写:
-
hashCode在Object - 结果
-
这个
MathContext哈希码 - 另请参见:
-
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
-
toString
public String toString()
返回此MathContext的字符串表示MathContext。 返回的String表示MathContext对象的设置,作为两个空格分隔的单词(由单个空格字符分隔,'\u0020',没有前导或尾随空格),如下所示:- 字符串
"precision=",紧接着是精度设置的值作为数字字符串,就像Integer.toString方法生成一样 。 - 字符串
"roundingMode=",紧随其后的值为roundingMode设置为一个字。 这个字将与RoundingMode枚举中对应的公共常量的名称相同。
例如:
precision=9 roundingMode=HALF_UP如果将更多属性添加到此类中,则可能会在未来添加toString的结果。 - 字符串
-
-