public class BigDecimal
extends Number implements Comparable<BigDecimal>, Serializable
| java.lang.Object | ||
| java.lang.Number | ||
| java.math.BigDecimal | ||
一个不可变的任意精度带符号的小数。
一个值由一个任意精度的“非标定值”和一个带符号的32位“标度”表示,组合为: unscaled * 10<sup>-scale</sup> 。 见unscaledValue()和scale() 。
大多数操作允许您提供 MathContext来指定所需的舍入模式。
常量(Constants) |
|
|---|---|
int |
ROUND_CEILING 舍入模式向正无穷大转弯。 |
int |
ROUND_DOWN 舍入模式,其中的值被舍入为零。 |
int |
ROUND_FLOOR 舍入模式向负无穷大转弯。 |
int |
ROUND_HALF_DOWN 舍入模式,其值向最近的邻居圆整。 |
int |
ROUND_HALF_EVEN 舍入模式,其值向最近的邻居圆整。 |
int |
ROUND_HALF_UP 舍入模式,其值向最近的邻居圆整。 |
int |
ROUND_UNNECESSARY 四舍五入模式下舍入操作抛出一个 |
int |
ROUND_UP 舍入模式,正值向正无穷大舍入,负值向负无穷大舍入。 |
Fields |
|
|---|---|
public static final BigDecimal |
ONE 作为 |
public static final BigDecimal |
TEN 常数十为 |
public static final BigDecimal |
ZERO 作为 |
Public constructors |
|
|---|---|
BigDecimal(char[] in, int offset, int len) 从作为字符数组给定的字符串表示构造一个新的 |
|
BigDecimal(char[] in, int offset, int len, MathContext mc) 从作为字符数组给定的字符串表示构造一个新的 |
|
BigDecimal(char[] in) 从作为字符数组给定的字符串表示构造一个新的 |
|
BigDecimal(char[] in, MathContext mc) 从作为字符数组给定的字符串表示构造一个新的 |
|
BigDecimal(String val) 从字符串表示构造一个新的 |
|
BigDecimal(String val, MathContext mc) 从字符串表示构造一个新的 |
|
BigDecimal(double val) 从64位double |
|
BigDecimal(double val, MathContext mc) 从64bit double |
|
BigDecimal(BigInteger val) 从给定的大整数 |
|
BigDecimal(BigInteger val, MathContext mc) 从给定的大整数 |
|
BigDecimal(BigInteger unscaledVal, int scale) 根据给定的非缩放值 |
|
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) 根据给定的未缩放值 |
|
BigDecimal(int val) 从给定的int |
|
BigDecimal(int val, MathContext mc) 从给定的int |
|
BigDecimal(long val) 从给定的长 |
|
BigDecimal(long val, MathContext mc) 从给定的长 |
|
公共方法(Public methods) |
|
|---|---|
BigDecimal |
abs(MathContext mc) 返回 |
BigDecimal |
abs() 返回 |
BigDecimal |
add(BigDecimal augend) 返回一个新的 |
BigDecimal |
add(BigDecimal augend, MathContext mc) 返回一个新的 |
byte |
byteValueExact() 如果它没有小数部分并且其值适合字节范围([-128..127]),则将此 |
int |
compareTo(BigDecimal val) 将此 |
BigDecimal |
divide(BigDecimal divisor, int scale, int roundingMode) 返回一个新的 |
BigDecimal |
divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 返回一个新的 |
BigDecimal |
divide(BigDecimal divisor, int roundingMode) 返回一个新的 |
BigDecimal |
divide(BigDecimal divisor, MathContext mc) 返回一个新的 |
BigDecimal |
divide(BigDecimal divisor, RoundingMode roundingMode) 返回一个新的 |
BigDecimal |
divide(BigDecimal divisor) 返回一个新的 |
BigDecimal[] |
divideAndRemainder(BigDecimal divisor, MathContext mc) 返回一个 |
BigDecimal[] |
divideAndRemainder(BigDecimal divisor) 返回一个 |
BigDecimal |
divideToIntegralValue(BigDecimal divisor, MathContext mc) 返回一个新 |
BigDecimal |
divideToIntegralValue(BigDecimal divisor) 返回一个新 |
double |
doubleValue() 将此 |
boolean |
equals(Object x) 如果 |
float |
floatValue() 将此 |
int |
hashCode() 返回此 |
int |
intValue() 以int值返回此 |
int |
intValueExact() 如果它没有小数部分并且它的值符合整型范围([-2 31 ..2 31 -1]),则将其作为int值返回此 |
long |
longValue() 将此 |
long |
longValueExact() 如果它没有小数部分并且它的值适合int范围([-2 63 ..2 63 -1]),则将此 |
BigDecimal |
max(BigDecimal val) 返回此 |
BigDecimal |
min(BigDecimal val) 返回此 |
BigDecimal |
movePointLeft(int n) 返回一个新的 |
BigDecimal |
movePointRight(int n) 返回一个新的 |
BigDecimal |
multiply(BigDecimal multiplicand, MathContext mc) 返回一个新的 |
BigDecimal |
multiply(BigDecimal multiplicand) 返回一个新的 |
BigDecimal |
negate() 返回一个新的 |
BigDecimal |
negate(MathContext mc) 返回一个新的 |
BigDecimal |
plus() 返回一个新的 |
BigDecimal |
plus(MathContext mc) 返回一个新的 |
BigDecimal |
pow(int n, MathContext mc) 返回一个新的 |
BigDecimal |
pow(int n) 返回一个新的 |
int |
precision() 返回此 |
BigDecimal |
remainder(BigDecimal divisor) 返回一个新的 |
BigDecimal |
remainder(BigDecimal divisor, MathContext mc) 返回一个新的 |
BigDecimal |
round(MathContext mc) 返回一个新的 |
int |
scale() 返回此 |
BigDecimal |
scaleByPowerOfTen(int n) 返回一个新的 |
BigDecimal |
setScale(int newScale, int roundingMode) 返回具有指定比例的新实例 |
BigDecimal |
setScale(int newScale) 返回具有指定比例的新实例 |
BigDecimal |
setScale(int newScale, RoundingMode roundingMode) 返回具有指定比例的新实例 |
short |
shortValueExact() 返回该 |
int |
signum() 返回这个 |
BigDecimal |
stripTrailingZeros() 返回一个新的 |
BigDecimal |
subtract(BigDecimal subtrahend) 返回一个新的 |
BigDecimal |
subtract(BigDecimal subtrahend, MathContext mc) 返回一个新的 |
BigInteger |
toBigInteger() 将此 |
BigInteger |
toBigIntegerExact() 如果它没有小数部分,则将此 |
String |
toEngineeringString() 返回此 |
String |
toPlainString() 返回此 |
String |
toString() 返回此 |
BigDecimal |
ulp() 返回此 |
BigInteger |
unscaledValue() 以 |
static BigDecimal |
valueOf(double val) 返回一个新的 |
static BigDecimal |
valueOf(long unscaledVal) 返回一个新的 |
static BigDecimal |
valueOf(long unscaledVal, int scale) 返回一个新的 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Number
|
|
java.lang.Object
|
|
java.lang.Comparable
|
|
int ROUND_CEILING
舍入模式向正无穷大转弯。 对于正值,该舍入模式表现为ROUND_UP ,负值表示为ROUND_DOWN 。
也可以看看:
常量值:2(0x00000002)
int ROUND_FLOOR
舍入模式向负无穷大转弯。 对于正值,该舍入模式表现为ROUND_DOWN ,负值表示为ROUND_UP 。
也可以看看:
常量值:3(0x00000003)
int ROUND_HALF_DOWN
舍入模式,其值向最近的邻居圆整。 领带被四舍五入打破。
也可以看看:
常量值:5(0x00000005)
int ROUND_HALF_EVEN
舍入模式,其值向最近的邻居圆整。 领带被四舍五入打败了邻居。
也可以看看:
常数值:6(0x00000006)
int ROUND_HALF_UP
舍入模式,其值向最近的邻居圆整。 领带被四舍五入打破。
也可以看看:
常量值:4(0x00000004)
int ROUND_UNNECESSARY
四舍五入模式,舍入操作为需要四舍五入的情况抛出 ArithmeticException ,也就是说,对于不能精确表示值的情况。
也可以看看:
常量值:7(0x00000007)
BigDecimal (char[] in,
int offset,
int len)
从作为字符数组给定的字符串表示构造一个新的 BigDecimal实例。
| 参数(Parameters) | |
|---|---|
in |
char: array of characters containing the string representation of this BigDecimal. |
offset |
int: first index to be copied. |
len |
int: number of characters to be used. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if offset < 0 || len <= 0 || offset+len-1 < 0 || offset+len-1 >= in.length, or if in does not contain a valid string representation of a big decimal. |
BigDecimal (char[] in,
int offset,
int len,
MathContext mc)
从作为字符数组给定的字符串表示构造一个新的 BigDecimal实例。
| 参数(Parameters) | |
|---|---|
in |
char: array of characters containing the string representation of this BigDecimal. |
offset |
int: first index to be copied. |
len |
int: number of characters to be used. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if offset < 0 || len <= 0 || offset+len-1 < 0 || offset+len-1 >= in.length, or if in does not contain a valid string representation of a big decimal. |
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
BigDecimal (char[] in)
从作为字符数组给定的字符串表示构造一个新的 BigDecimal实例。
| 参数(Parameters) | |
|---|---|
in |
char: array of characters containing the string representation of this BigDecimal. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if in does not contain a valid string representation of a big decimal. |
BigDecimal (char[] in,
MathContext mc)
从作为字符数组给定的字符串表示构造一个新的BigDecimal实例。 结果根据指定的数学上下文进行四舍五入。
| 参数(Parameters) | |
|---|---|
in |
char: array of characters containing the string representation of this BigDecimal. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if in does not contain a valid string representation of a big decimal. |
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
BigDecimal (String val)
从字符串表示构造一个新的 BigDecimal实例。
| 参数(Parameters) | |
|---|---|
val |
String
|
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if val does not contain a valid string representation of a big decimal. |
BigDecimal (String val, MathContext mc)
从字符串表示构造一个新的BigDecimal实例。 结果根据指定的数学上下文进行四舍五入。
| 参数(Parameters) | |
|---|---|
val |
String
|
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if val does not contain a valid string representation of a big decimal. |
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
BigDecimal (double val)
从64bit double val构造一个新的BigDecimal实例。 构造的大数小数相当于给定的double。 例如, new BigDecimal(0.1)等于0.1000000000000000055511151231257827021181583404541015625 。 这是因为0.1不能完全用二进制表示。
要生成等效于 0.1的大十进制实例, 0.1使用 BigDecimal(String)构造函数。
| 参数(Parameters) | |
|---|---|
val |
double: double value to be converted to a BigDecimal instance. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if val is infinity or not a number. |
BigDecimal (double val,
MathContext mc)
从64位双val构造一个新的BigDecimal实例。 构造的大数小数相当于给定的double。 例如, new BigDecimal(0.1)等于0.1000000000000000055511151231257827021181583404541015625 。 这是因为0.1不能完全用二进制表示。
要生成等效于 0.1的大十进制实例, 0.1使用 BigDecimal(String)构造函数。
| 参数(Parameters) | |
|---|---|
val |
double: double value to be converted to a BigDecimal instance. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if val is infinity or not a number. |
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
BigDecimal (BigInteger val)
从给定的大整数val构造一个新的BigDecimal实例。 结果的比例是0 。
| 参数(Parameters) | |
|---|---|
val |
BigInteger
|
BigDecimal (BigInteger val, MathContext mc)
从给定的大整数val构造一个新的BigDecimal实例。 结果的比例是0 。
| 参数(Parameters) | |
|---|---|
val |
BigInteger
|
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
BigDecimal (BigInteger unscaledVal, int scale)
根据给定的未缩放值unscaledVal和给定的比例构造一个新的BigDecimal实例。 这个实例的值是unscaledVal * 10<sup>-scale</sup> )。
| 参数(Parameters) | |
|---|---|
unscaledVal |
BigInteger
|
scale |
int
|
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if unscaledVal == null. |
BigDecimal (BigInteger unscaledVal, int scale, MathContext mc)
根据给定的非缩放值unscaledVal和给定的比例构造一个新的BigDecimal实例。 这个实例的值是{@code unscaledVal * 10 -scale )。 结果根据指定的数学上下文进行四舍五入。
| 参数(Parameters) | |
|---|---|
unscaledVal |
BigInteger
|
scale |
int
|
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
NullPointerException |
if unscaledVal == null. |
BigDecimal (int val)
从给定的int val构造一个新的BigDecimal实例。 结果的比例是0。
| 参数(Parameters) | |
|---|---|
val |
int: int value to be converted to a BigDecimal instance. |
BigDecimal (int val,
MathContext mc)
从给定的int val构造一个新的BigDecimal实例。 结果的比例是0 。 结果根据指定的数学上下文进行四舍五入。
| 参数(Parameters) | |
|---|---|
val |
int: int value to be converted to a BigDecimal instance. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if mc.precision > 0 and c.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
BigDecimal (long val)
从给定的长val构造一个新的BigDecimal实例。 结果的比例是0 。
| 参数(Parameters) | |
|---|---|
val |
long: long value to be converted to a BigDecimal instance. |
BigDecimal (long val,
MathContext mc)
从给定的长val构造一个新的BigDecimal实例。 结果的比例是0 。 结果根据指定的数学上下文进行四舍五入。
| 参数(Parameters) | |
|---|---|
val |
long: long value to be converted to a BigDecimal instance. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and the new big decimal cannot be represented within the given precision without rounding. |
BigDecimal abs (MathContext mc)
返回BigDecimal其值为绝对值this 。 结果根据传递的上下文mc 。
| 参数(Parameters) | |
|---|---|
mc |
MathContext
|
| 返回(Returns) | |
|---|---|
BigDecimal |
|
BigDecimal abs ()
返回BigDecimal其值为绝对值this 。 结果的规模与此规模相同。
| 返回(Returns) | |
|---|---|
BigDecimal |
|
BigDecimal add (BigDecimal augend)
返回一个新的BigDecimal其值为this + augend 。 结果的比例是两个参数的最大比例。
| 参数(Parameters) | |
|---|---|
augend |
BigDecimal: value to be added to this. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this + augend. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if augend == null. |
BigDecimal add (BigDecimal augend, MathContext mc)
返回一个新的BigDecimal其值为this + augend 。 结果根据传递的上下文mc 。
| 参数(Parameters) | |
|---|---|
augend |
BigDecimal: value to be added to this. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this + augend. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if augend == null or mc == null. |
byte byteValueExact ()
如果它没有小数部分并且其值适合字节范围([-128..127]),则将此BigDecimal作为字节值返回。 如果这些条件不符合,则抛出ArithmeticException 。
| 返回(Returns) | |
|---|---|
byte |
|
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if rounding is necessary or the number doesn't fit in a byte. |
int compareTo (BigDecimal val)
将此BigDecimal与val比较。 返回三个值1 0或-1 。 该方法的行为如同计算this.subtract(val) 。 如果差值大于0,则返回1,如果差值<0,则返回-1,如果差值为0,则返回0。 这意味着,如果比较两个十进制实例的值相等但规模不同,则认为这两个实例是相等的。
| 参数(Parameters) | |
|---|---|
val |
BigDecimal: value to be compared with this. |
| 返回(Returns) | |
|---|---|
int |
1 if this > val, -1 if this < val, 0 if this == val. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if val == null. |
BigDecimal divide (BigDecimal divisor, int scale, int roundingMode)
返回一个新的BigDecimal其值为this / divisor 。 作为结果的比例,使用参数scale 。 如果舍入要求符合指定的比例,则应用指定的舍入模式roundingMode 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
scale |
int: the scale of the result returned. |
roundingMode |
int: rounding mode to be used to round the result. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this / divisor rounded according to the given rounding mode. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
IllegalArgumentException |
if roundingMode is not a valid rounding mode. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if roundingMode == ROUND_UNNECESSARY and rounding is necessary according to the given scale. |
BigDecimal divide (BigDecimal divisor, int scale, RoundingMode roundingMode)
返回一个新的BigDecimal其值为this / divisor 。 作为结果的比例,使用参数scale 。 如果圆整需要符合指定的比例,则应用指定的舍入模式roundingMode 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
scale |
int: the scale of the result returned. |
roundingMode |
RoundingMode: rounding mode to be used to round the result. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this / divisor rounded according to the given rounding mode. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null or roundingMode == null. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if roundingMode == RoundingMode.UNNECESSARY and rounding is necessary according to the given scale and given precision. |
BigDecimal divide (BigDecimal divisor, int roundingMode)
返回一个新的BigDecimal其值为this / divisor 。 结果的比例是this的比例。 如果舍入要求符合指定的比例,则应用指定的舍入模式roundingMode 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
roundingMode |
int: rounding mode to be used to round the result. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this / divisor rounded according to the given rounding mode. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
IllegalArgumentException |
if roundingMode is not a valid rounding mode. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if roundingMode == ROUND_UNNECESSARY and rounding is necessary according to the scale of this. |
BigDecimal divide (BigDecimal divisor, MathContext mc)
返回一个新的BigDecimal其值为this / divisor 。 结果根据传递的上下文mc 。 如果通过的数学上下文指定精度0 ,则此调用等同于this.divide(divisor) 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this / divisor. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null or mc == null. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if mc.getRoundingMode() == UNNECESSARY and rounding is necessary according mc.getPrecision(). |
BigDecimal divide (BigDecimal divisor, RoundingMode roundingMode)
返回一个新的BigDecimal其值为this / divisor 。 结果的比例是this的比例。 如果舍入要求达到指定的比例,则应用指定的舍入模式roundingMode 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
roundingMode |
RoundingMode: rounding mode to be used to round the result. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this / divisor rounded according to the given rounding mode. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null or roundingMode == null. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if roundingMode == RoundingMode.UNNECESSARY and rounding is necessary according to the scale of this. |
BigDecimal divide (BigDecimal divisor)
返回一个新的BigDecimal其值为this / divisor 。 结果的比例是this和divisor的比例divisor 。 如果确切的结果需要更多的数字,则相应地调整比例。 例如, 1/128 = 0.0078125其规模为7 ,精度为5 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this / divisor. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if the result cannot be represented exactly. |
BigDecimal[] divideAndRemainder (BigDecimal divisor, MathContext mc)
返回一个BigDecimal数组,其中包含索引0处的this / divisor的整数部分和索引1处的剩余部分this % divisor商向下舍入到下一个整数。 未考虑通过参数mc传递的舍入模式。 但是,如果mc > 0的精度和积分部分需要更多数字,则会抛出ArithmeticException 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
mc |
MathContext: math context which determines the maximal precision of the result. |
| 返回(Returns) | |
|---|---|
BigDecimal[] |
[this.divideToIntegralValue(divisor), this.remainder(divisor)]. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
ArithmeticException |
if divisor == 0. |
BigDecimal[] divideAndRemainder (BigDecimal divisor)
返回一个 BigDecimal数组,其中包含索引0处的 this / divisor的整数部分,索引1处的剩余部分为 this % divisor商向下舍入到下一个整数。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
| 返回(Returns) | |
|---|---|
BigDecimal[] |
[this.divideToIntegralValue(divisor), this.remainder(divisor)]. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
ArithmeticException |
if divisor == 0. |
BigDecimal divideToIntegralValue (BigDecimal divisor, MathContext mc)
返回一个新BigDecimal ,其值是的组成部分this / divisor 。 商向下舍入到下一个整数。 未考虑通过参数mc传递的舍入模式。 但是,如果mc > 0的精度和整数部分需要更多数字,则会引发ArithmeticException 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
mc |
MathContext: math context which determines the maximal precision of the result. |
| 返回(Returns) | |
|---|---|
BigDecimal |
integral part of this / divisor. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null or mc == null. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if mc.getPrecision() > 0 and the result requires more digits to be represented. |
BigDecimal divideToIntegralValue (BigDecimal divisor)
返回一个新BigDecimal ,其值是的组成部分this / divisor 。 商向下舍入到下一个整数。 例如, 0.5/0.2 = 2 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
| 返回(Returns) | |
|---|---|
BigDecimal |
integral part of this / divisor. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
ArithmeticException |
if divisor == 0. |
double doubleValue ()
将此BigDecimal作为双BigDecimal值返回。 如果this太大而无法表示为浮点数,则返回Double.POSITIVE_INFINITY或Double.NEGATIVE_INFINITY 。
请注意,如果未缩放的值有超过53个有效数字,那么这个小数不能完全表示为双变量。 在这种情况下,结果是四舍五入的。
例如,如果实例 x1 = new BigDecimal("0.1")不能被精确表示为双,因此 x1.equals(new BigDecimal(x1.doubleValue())返回 false对于这种情况。
同样,如果实例 new BigDecimal(9007199254740993L)转换为double,则结果为 9.007199254740992E15 。
| 返回(Returns) | |
|---|---|
double |
this BigDecimal as a double value. |
boolean equals (Object x)
如果x是BigDecimal实例并且此实例等于这个大小数,则返回true 。 如果它们的非缩放值和它们的比例相等,那么两个大的小数是相等的。 例如,1.0(10 * 10 -1 )不等于1.00(100 * 10 -2 )。 同样,如果零度不同,零实例也不相等。
| 参数(Parameters) | |
|---|---|
x |
Object: the reference object with which to compare. |
| 返回(Returns) | |
|---|---|
boolean |
true if this object is the same as the obj argument; false otherwise. |
float floatValue ()
将此BigDecimal作为浮点值返回。 如果this太大而无法表示为浮点数,则返回Float.POSITIVE_INFINITY或Float.NEGATIVE_INFINITY 。
请注意,如果未缩放的值的有效位数超过24位,那么这个小数不能完全表示为浮点型变量。 在这种情况下,结果是四舍五入的。
例如,如果实例 x1 = new BigDecimal("0.1")不能完全表示为浮点数, false对于此情况, x1.equals(new BigDecimal(x1.floatValue())将返回 false 。
同样,如果实例 new BigDecimal(16777217)转换为浮点型,则结果为 1.6777216E 7。
| 返回(Returns) | |
|---|---|
float |
this BigDecimal as a float value. |
int hashCode ()
返回此 BigDecimal的哈希码。
| 返回(Returns) | |
|---|---|
int |
hash code for this. |
int intValue ()
将此BigDecimal作为int值返回。 任何小数部分被丢弃。 如果this的整数部分太大而不能表示为int,则返回this % 2<sup>32</sup> 。
| 返回(Returns) | |
|---|---|
int |
the numeric value represented by this object after conversion to type int. |
int intValueExact ()
如果它没有小数部分并且它的值符合整型范围([-2 31 ..2 31 -1]),则将此BigDecimal作为int值返回。 如果这些条件不符合,则抛出ArithmeticException 。
| 返回(Returns) | |
|---|---|
int |
|
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if rounding is necessary or the number doesn't fit in an int. |
long longValue ()
以长BigDecimal返回此BigDecimal 。 任何小数部分被丢弃。 如果this的整数部分太大而无法表示为长this % 2<sup>64</sup>则返回this % 2<sup>64</sup> 。
| 返回(Returns) | |
|---|---|
long |
the numeric value represented by this object after conversion to type long. |
long longValueExact ()
如果它没有小数部分并且它的值符合int范围([-2 63 ..2 63 -1]),则将此BigDecimal作为长BigDecimal值返回。 如果这些条件不符合,则抛出ArithmeticException 。
| 返回(Returns) | |
|---|---|
long |
|
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if rounding is necessary or the number doesn't fit in a long. |
BigDecimal max (BigDecimal val)
返回此 BigDecimal和 val 。
| 参数(Parameters) | |
|---|---|
val |
BigDecimal: value to be used to compute the maximum with this. |
| 返回(Returns) | |
|---|---|
BigDecimal |
max(this, val. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if val == null. |
BigDecimal min (BigDecimal val)
返回此 BigDecimal和 val 。
| 参数(Parameters) | |
|---|---|
val |
BigDecimal: value to be used to compute the minimum with this. |
| 返回(Returns) | |
|---|---|
BigDecimal |
min(this, val. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if val == null. |
BigDecimal movePointLeft (int n)
返回一个新的BigDecimal实例,其中小数点已移至n位置左侧。 如果n < 0那么小数点-n 。
结果是通过改变其规模来获得的。 如果结果的比例变为负值,则其精度增加,以使比例为零。
请注意, movePointLeft(0)返回数学上等效的结果,但其中包含 scale >= 0 。
| 参数(Parameters) | |
|---|---|
n |
int
|
| 返回(Returns) | |
|---|---|
BigDecimal |
|
BigDecimal movePointRight (int n)
返回一个新的BigDecimal实例,其中小数点已被移到n位置右侧。 如果n < 0那么小数点将-n到左侧。
结果是通过改变其规模来获得的。 如果结果的比例变为负值,则其精度增加,以使比例为零。
请注意, movePointRight(0)返回数学上等效的结果,但其比例> = 0。
| 参数(Parameters) | |
|---|---|
n |
int
|
| 返回(Returns) | |
|---|---|
BigDecimal |
|
BigDecimal multiply (BigDecimal multiplicand, MathContext mc)
返回一个新的BigDecimal其值为this * multiplicand 。 结果根据传递的上下文mc 。
| 参数(Parameters) | |
|---|---|
multiplicand |
BigDecimal: value to be multiplied with this. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this * multiplicand. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if multiplicand == null or mc == null. |
BigDecimal multiply (BigDecimal multiplicand)
返回一个新的BigDecimal其值为this * multiplicand 。 结果的尺度是两个参数的尺度的总和。
| 参数(Parameters) | |
|---|---|
multiplicand |
BigDecimal: value to be multiplied with this. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this * multiplicand. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if multiplicand == null. |
BigDecimal negate ()
返回一个新的BigDecimal其值为-this 。 结果的规模与此规模相同。
| 返回(Returns) | |
|---|---|
BigDecimal |
-this |
BigDecimal negate (MathContext mc)
返回一个新的BigDecimal其值为-this 。 结果根据传递的上下文mc 。
| 参数(Parameters) | |
|---|---|
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
-this |
BigDecimal plus ()
返回一个新的BigDecimal其值为+this 。 结果的规模与此规模相同。
| 返回(Returns) | |
|---|---|
BigDecimal |
this |
BigDecimal plus (MathContext mc)
返回一个新的BigDecimal其值为+this 。 结果根据传递的上下文mc 。
| 参数(Parameters) | |
|---|---|
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this, rounded |
BigDecimal pow (int n, MathContext mc)
返回一个新BigDecimal ,其值是this<sup>n</sup> 。 结果根据传递的上下文mc 。
实现注意:该实现基于ANSI标准X3.274-1996算法。
| 参数(Parameters) | |
|---|---|
n |
int
|
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
|
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if n < 0 or n > 999999999. |
BigDecimal pow (int n)
返回一个新的BigDecimal其值为this<sup>n</sup> 。 结果的比例是n * this.scale() 。
x.pow(0)返回 1 ,即使 x == 0 。
实现注意:该实现基于ANSI标准X3.274-1996算法。
| 参数(Parameters) | |
|---|---|
n |
int
|
| 返回(Returns) | |
|---|---|
BigDecimal |
|
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if n < 0 or n > 999999999. |
int precision ()
返回此BigDecimal的精度。 精度是用来表示这个小数的小数位数。 它相当于未缩放值的位数。 0的精度为1 (与规模无关)。
| 返回(Returns) | |
|---|---|
int |
the precision of this BigDecimal. |
BigDecimal remainder (BigDecimal divisor)
返回一个新 BigDecimal ,其值是 this % divisor 。
其余部分定义为 this - this.divideToIntegralValue(divisor) * divisor 。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this % divisor. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
ArithmeticException |
if divisor == 0. |
BigDecimal remainder (BigDecimal divisor, MathContext mc)
返回一个新的 BigDecimal其值为 this % divisor 。
其余部分定义为 this - this.divideToIntegralValue(divisor) * divisor 。
指定的舍入模式 mc仅用于除法。
| 参数(Parameters) | |
|---|---|
divisor |
BigDecimal: value by which this is divided. |
mc |
MathContext: rounding mode and precision to be used. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this % divisor. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if divisor == null. |
ArithmeticException |
if divisor == 0. |
ArithmeticException |
if mc.getPrecision() > 0 and the result of this.divideToIntegralValue(divisor, mc) requires more digits to be represented. |
BigDecimal round (MathContext mc)
返回一个新的 BigDecimal其值为 this ,根据传递的上下文 mc 。
如果 mc.precision = 0 ,则不执行舍入。
如果 mc.precision > 0和 mc.roundingMode == UNNECESSARY ,则 ArithmeticException如果结果不能完全在给定精度内表示,则抛出 ArithmeticException 。
| 参数(Parameters) | |
|---|---|
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this rounded according to the passed context. |
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if mc.precision > 0 and mc.roundingMode == UNNECESSARY and this cannot be represented within the given precision. |
int scale ()
返回此BigDecimal的比例。 比例是小数点后面的位数。 这BigDecimal的价值是unsignedValue * 10<sup>-scale</sup> 。 如果比例是负数,那么这个BigDecimal代表一个大整数。
| 返回(Returns) | |
|---|---|
int |
the scale of this BigDecimal. |
BigDecimal scaleByPowerOfTen (int n)
返回一个新的BigDecimal其值为this * 10<sup>n</sup> 。 结果的比例是this.scale() - n 。 结果的精度是this的精度。
此方法与 movePointRight(int)具有相同的效果,但不改变精度。
| 参数(Parameters) | |
|---|---|
n |
int
|
| 返回(Returns) | |
|---|---|
BigDecimal |
|
BigDecimal setScale (int newScale, int roundingMode)
返回具有指定比例的新实例 BigDecimal 。
如果新比例大于旧比例,则将另外的零添加到非比例值。 在这种情况下,不需要舍入。
如果新比例小于旧比例,则删除尾数。 如果这些尾部数字不为零,则剩余的未缩放值必须舍入。 对于该舍入操作,使用指定的舍入模式。
| 参数(Parameters) | |
|---|---|
newScale |
int: scale of the result returned. |
roundingMode |
int: rounding mode to be used to round the result. |
| 返回(Returns) | |
|---|---|
BigDecimal |
a new BigDecimal instance with the specified scale. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if roundingMode is not a valid rounding mode. |
ArithmeticException |
if roundingMode == ROUND_UNNECESSARY and rounding is necessary according to the given scale. |
BigDecimal setScale (int newScale)
返回具有指定比例的新实例BigDecimal 。 如果新比例大于旧比例,则将另外的零添加到非比例值。 如果新比例小于旧比例,则删除尾部零。 如果尾数字不是零,则抛出ArithmeticException。
如果没有抛出异常,则以下等式成立: x.setScale(s).compareTo(x) == 0 。
| 参数(Parameters) | |
|---|---|
newScale |
int: scale of the result returned. |
| 返回(Returns) | |
|---|---|
BigDecimal |
a new BigDecimal instance with the specified scale. |
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if rounding would be necessary. |
BigDecimal setScale (int newScale, RoundingMode roundingMode)
返回具有指定比例的新实例 BigDecimal 。
如果新比例大于旧比例,则将另外的零添加到非比例值。 在这种情况下,不需要舍入。
如果新比例小于旧比例,则删除尾数。 如果这些尾部数字不为零,则剩余的未缩放值必须舍入。 对于该舍入操作,使用指定的舍入模式。
| 参数(Parameters) | |
|---|---|
newScale |
int: scale of the result returned. |
roundingMode |
RoundingMode: rounding mode to be used to round the result. |
| 返回(Returns) | |
|---|---|
BigDecimal |
a new BigDecimal instance with the specified scale. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if roundingMode == null. |
ArithmeticException |
if roundingMode == ROUND_UNNECESSARY and rounding is necessary according to the given scale. |
short shortValueExact ()
返回该BigDecimal ,如果它没有小数部分和短的值,如果其值适配于短程([-2 15 ..2 15 -1])。 如果这些条件不满足,则抛出ArithmeticException 。
| 返回(Returns) | |
|---|---|
short |
|
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if rounding is necessary of the number doesn't fit in a short. |
int signum ()
返回此 BigDecimal的符号。
| 返回(Returns) | |
|---|---|
int |
-1 if this < 0, 0 if this == 0, 1 if this > 0. |
BigDecimal stripTrailingZeros ()
返回一个新的BigDecimal实例,其值与this值相同,但具有已去除尾部零的非缩放值。 如果this的非缩放值有n个尾随零,则结果的比例和精度已降低n。
| 返回(Returns) | |
|---|---|
BigDecimal |
a new BigDecimal instance equivalent to this where the trailing zeros of the unscaled value have been removed. |
BigDecimal subtract (BigDecimal subtrahend)
返回一个新的BigDecimal其值为this - subtrahend 。 结果的比例是两个参数的最大比例。
| 参数(Parameters) | |
|---|---|
subtrahend |
BigDecimal: value to be subtracted from this. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this - subtrahend. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if subtrahend == null. |
BigDecimal subtract (BigDecimal subtrahend, MathContext mc)
返回一个新的BigDecimal其值为this - subtrahend 。 结果根据传递的上下文mc 。
| 参数(Parameters) | |
|---|---|
subtrahend |
BigDecimal: value to be subtracted from this. |
mc |
MathContext: rounding mode and precision for the result of this operation. |
| 返回(Returns) | |
|---|---|
BigDecimal |
this - subtrahend. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if subtrahend == null or mc == null. |
BigInteger toBigInteger ()
将此BigDecimal作为大整数实例返回。 小数部分被丢弃。
| 返回(Returns) | |
|---|---|
BigInteger |
this BigDecimal as a big integer instance. |
BigInteger toBigIntegerExact ()
如果它没有小数部分,则将此BigDecimal作为大整数实例返回。 如果这个BigDecimal有一个小数部分,也就是说如果需要四舍五入, ArithmeticException抛出一个ArithmeticException 。
| 返回(Returns) | |
|---|---|
BigInteger |
this BigDecimal as a big integer value. |
| 抛出异常(Throws) | |
|---|---|
ArithmeticException |
if rounding is necessary. |
String toEngineeringString ()
返回此BigDecimal的字符串表示BigDecimal 。 此表示总是打印此值的所有有效数字。
如果比例为负数或者scale - precision >= 6则使用工程符号。 工程表示法与科学表示法类似,只不过指数是3的倍数,因此整数部分大于等于1且小于等于1000。
| 返回(Returns) | |
|---|---|
String |
a string representation of this in engineering notation if necessary. |
String toPlainString ()
返回此BigDecimal的字符串表示BigDecimal 。 没有使用科学记数法。 此方法在必要时添加零。
如果使用此字符串表示形式创建新实例,则此实例通常与精度更改时的 this不相同。
x.equals(new BigDecimal(x.toPlainString())通常会返回 false 。
x.compareTo(new BigDecimal(x.toPlainString())返回 0 。
| 返回(Returns) | |
|---|---|
String |
a string representation of this without exponent part. |
String toString ()
返回此BigDecimal的规范字符串表示BigDecimal 。 如有必要,使用科学记数法。 此表示总是打印此值的所有有效数字。
如果比例是负值或者 scale - precision >= 6那么使用科学记数法。
| 返回(Returns) | |
|---|---|
String |
a string representation of this in scientific notation if necessary. |
BigDecimal ulp ()
返回此BigDecimal实例的最后一位(ULP)中的BigDecimal 。 ULP是具有相同精度的最接近的十进制小数的距离。
浮点运算评估中舍入误差的数量通常用ULP表示。 1 ULP的错误通常被视为可容忍的错误。
对于课程BigDecimal ,一个数字的ULP只是10 比例 。 例如, new BigDecimal(0.1).ulp()返回1E-55 。
| 返回(Returns) | |
|---|---|
BigDecimal |
unit in the last place (ULP) of this BigDecimal instance. |
BigInteger unscaledValue ()
以BigInteger返回此BigDecimal实例的未缩放值(尾数)。 未缩放的值可以计算为this * 10<sup>scale</sup> 。
| 返回(Returns) | |
|---|---|
BigInteger |
|
BigDecimal valueOf (double val)
返回一个新的BigDecimal实例,其值等于val 。 新的十进制的构造就好像调用BigDecimal(String)构造函数的参数等于Double.toString(val) 。 例如, valueOf("0.1")被转换为(无标度= 1,标度= 1),尽管双0.1值0.1不能完全表示为双0.1值。 与此相反,新的BigDecimal(0.1)实例的值为0.1000000000000000055511151231257827021181583404541015625其中未缩放的值为1000000000000000055511151231257827021181583404541015625 ,比例为55 。
| 参数(Parameters) | |
|---|---|
val |
double: double value to be converted to a BigDecimal. |
| 返回(Returns) | |
|---|---|
BigDecimal |
BigDecimal instance with the value val. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if val is infinite or val is not a number |
BigDecimal valueOf (long unscaledVal)
返回一个新的BigDecimal实例,其值等于unscaledVal 。 结果的比例是0 ,其非缩放值是unscaledVal 。
| 参数(Parameters) | |
|---|---|
unscaledVal |
long: value to be converted to a BigDecimal. |
| 返回(Returns) | |
|---|---|
BigDecimal |
BigDecimal instance with the value unscaledVal. |
BigDecimal valueOf (long unscaledVal, int scale)
返回一个新的BigDecimal实例,其值等于unscaledVal * 10<sup>-scale</sup> )。 结果的比例是scale ,其未缩放值是unscaledVal 。
| 参数(Parameters) | |
|---|---|
unscaledVal |
long
|
scale |
int
|
| 返回(Returns) | |
|---|---|
BigDecimal |
|