public final class MathContext extends Object implements Serializable
BigDecimal类实现的那些规则。
独立于基地的设置有:
precision :用于操作的位数; 结果四舍五入到这个精度 roundingMode :一个RoundingMode对象,它指定用于舍入的算法。 BigDecimal , RoundingMode , Serialized Form
| Modifier and Type | Field and Description |
|---|---|
static MathContext |
DECIMAL128
A
MathContext对象,其精度设置与IEEE 754R Decimal128格式,34位数字和
HALF_EVEN的舍入模式(IEEE 754R默认值)匹配。
|
static MathContext |
DECIMAL32
A
MathContext对象,其精度设置与IEEE 754R Decimal32格式,7位数字以及
HALF_EVEN的舍入模式(IEEE 754R默认值)匹配。
|
static MathContext |
DECIMAL64
|
static MathContext |
UNLIMITED
一个
MathContext对象,其设置具有无限精度算术所需的值。
|
| Constructor and Description |
|---|
MathContext(int setPrecision)
构造一个新的
MathContext具有指定的精度和
HALF_UP舍入模式。
|
MathContext(int setPrecision, RoundingMode setRoundingMode)
构造一个新的
MathContext ,具有指定的精度和舍入模式。
|
MathContext(String val)
从一个字符串构造一个新的
MathContext 。
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object x)
将此
MathContext与指定的
Object进行比较以获得相等性。
|
int |
getPrecision()
返回
precision设置。
|
RoundingMode |
getRoundingMode()
返回roundingMode设置。
|
int |
hashCode()
返回此
MathContext的哈希码。
|
String |
toString()
返回此
MathContext的字符串表示
MathContext 。
|
public static final MathContext UNLIMITED
MathContext对象,其设置具有无限精度算术所需的值。
设置的值为: precision=0 roundingMode=HALF_UP
public static final MathContext DECIMAL32
public static final MathContext DECIMAL64
MathContext对象,其精度设置符合IEEE 754R Decimal64格式,16位数字和
HALF_EVEN的舍入模式,IEEE 754R默认值。
public static final MathContext DECIMAL128
MathContext对象,其精度设置与IEEE 754R Decimal128格式,34位数字和
HALF_EVEN的舍入模式(IEEE 754R默认值)相匹配。
public MathContext(int setPrecision)
HALF_UP舍入模式的新
MathContext 。
setPrecision - 非负
int精度设置。
IllegalArgumentException - 如果
setPrecision参数小于零。
public MathContext(int setPrecision,
RoundingMode setRoundingMode)
MathContext具有指定的精度和舍入模式。
setPrecision - 非负
int精度设置。
setRoundingMode - 要使用的舍入模式。
IllegalArgumentException - 如果
setPrecision参数小于零。
NullPointerException - 如果舍入模式参数为
null
public MathContext(String val)
MathContext 。
字符串的格式必须与toString()方法相同。
一个IllegalArgumentException被抛出如果字符串的精度部分超出范围( < 0 )或字符串不是在由创建的格式toString()方法。
val - 要解析的字符串
IllegalArgumentException - 如果精度部分超出范围或格式错误
NullPointerException - 如果参数是
null
public int getPrecision()
precision设置。
该值始终为非负数。
int这是
precision设置的值
public RoundingMode getRoundingMode()
RoundingMode.CEILING , RoundingMode.DOWN , RoundingMode.FLOOR , RoundingMode.HALF_DOWN , RoundingMode.HALF_EVEN , RoundingMode.HALF_UP , RoundingMode.UNNECESSARY ,或RoundingMode.UP 。
RoundingMode对象,它是
roundingMode设置的值
public boolean equals(Object x)
MathContext与指定的
Object进行比较以获得相等性。
equals在
Object
x -
Object要与此
MathContext进行比较。
true当且仅当指定的
Object是一个
MathContext对象,其具有与该对象完全相同的设置
Object.hashCode() , HashMap
public int hashCode()
MathContext的哈希码。
hashCode在
Object类
MathContext哈希码
Object.equals(java.lang.Object) ,
System.identityHashCode(java.lang.Object)
public String toString()
MathContext的字符串表示MathContext 。
String返回的表示MathContext对象的设置,作为两个空格分隔的单词(由单个空格字符分隔, '\u0020' ,没有前导或尾随空格),如下所示:
"precision=" ,紧随其后的精度设置值为数字字符串,如同由Integer.toString方法生成的。 "roundingMode=" ,紧随其后的是roundingMode设置为一个字。 这个字将与RoundingMode枚举中对应的公共常量的名称相同。 例如:
precision=9 roundingMode=HALF_UP
如果将更多的属性添加到此类中,则可能会在将来追加toString的结果。
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.