Class BigDecimal
- All Implemented Interfaces:
Serializable,Comparable<BigDecimal>
public class BigDecimal extends Number implements Comparable<BigDecimal>, Serializable
A value is represented by an arbitrary-precision "unscaled value" and a signed 32-bit "scale",
combined thus: unscaled * 10<sup>-scale</sup>. See unscaledValue() and scale.
Most operations allow you to supply a MathContext to specify a desired rounding mode.
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static BigDecimalONEThe constant one as aBigDecimal.static intROUND_CEILINGRounding mode to round towards positive infinity.static intROUND_DOWNRounding mode where the values are rounded towards zero.static intROUND_FLOORRounding mode to round towards negative infinity.static intROUND_HALF_DOWNRounding mode where values are rounded towards the nearest neighbor.static intROUND_HALF_EVENRounding mode where values are rounded towards the nearest neighbor.static intROUND_HALF_UPRounding mode where values are rounded towards the nearest neighbor.static intROUND_UNNECESSARYRounding mode where the rounding operations throws anArithmeticExceptionfor the case that rounding is necessary, i.e.static intROUND_UPRounding mode where positive values are rounded towards positive infinity and negative values towards negative infinity.static BigDecimalTENThe constant ten as aBigDecimal.static BigDecimalZEROThe constant zero as aBigDecimal. -
Constructor Summary
Constructors Constructor Description BigDecimal(char[] in)Constructs a newBigDecimalinstance from a string representation given as a character array.BigDecimal(char[] in, int offset, int len)Constructs a newBigDecimalinstance from a string representation given as a character array.BigDecimal(char[] in, int offset, int len, MathContext mc)Constructs a newBigDecimalinstance from a string representation given as a character array.BigDecimal(char[] in, MathContext mc)Constructs a newBigDecimalinstance from a string representation given as a character array.BigDecimal(double val)Constructs a newBigDecimalinstance from the 64bit doubleval.BigDecimal(double val, MathContext mc)Constructs a newBigDecimalinstance from the 64bit doubleval.BigDecimal(int val)Constructs a newBigDecimalinstance from the given intval.BigDecimal(int val, MathContext mc)Constructs a newBigDecimalinstance from the given intval.BigDecimal(long val)Constructs a newBigDecimalinstance from the given longval.BigDecimal(long val, MathContext mc)Constructs a newBigDecimalinstance from the given longval.BigDecimal(String val)Constructs a newBigDecimalinstance from a string representation.BigDecimal(String val, MathContext mc)Constructs a newBigDecimalinstance from a string representation.BigDecimal(BigInteger val)Constructs a newBigDecimalinstance from the given big integerval.BigDecimal(BigInteger unscaledVal, int scale)Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale.BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale.BigDecimal(BigInteger val, MathContext mc)Constructs a newBigDecimalinstance from the given big integerval. -
Method Summary
Modifier and Type Method Description BigDecimalabs()Returns aBigDecimalwhose value is the absolute value ofthis.BigDecimalabs(MathContext mc)Returns aBigDecimalwhose value is the absolute value ofthis.BigDecimaladd(BigDecimal augend)Returns a newBigDecimalwhose value isthis + augend.BigDecimaladd(BigDecimal augend, MathContext mc)Returns a newBigDecimalwhose value isthis + augend.bytebyteValueExact()Returns thisBigDecimalas a byte value if it has no fractional part and if its value fits to the byte range ([-128..127]).intcompareTo(BigDecimal val)Compares thisBigDecimalwithval.BigDecimaldivide(BigDecimal divisor)Returns a newBigDecimalwhose value isthis / divisor.BigDecimaldivide(BigDecimal divisor, int roundingMode)Returns a newBigDecimalwhose value isthis / divisor.BigDecimaldivide(BigDecimal divisor, int scale, int roundingMode)Returns a newBigDecimalwhose value isthis / divisor.BigDecimaldivide(BigDecimal divisor, int scale, RoundingMode roundingMode)Returns a newBigDecimalwhose value isthis / divisor.BigDecimaldivide(BigDecimal divisor, MathContext mc)Returns a newBigDecimalwhose value isthis / divisor.BigDecimaldivide(BigDecimal divisor, RoundingMode roundingMode)Returns a newBigDecimalwhose value isthis / divisor.BigDecimal[]divideAndRemainder(BigDecimal divisor)Returns aBigDecimalarray which contains the integral part ofthis / divisorat index 0 and the remainderthis % divisorat index 1.BigDecimal[]divideAndRemainder(BigDecimal divisor, MathContext mc)Returns aBigDecimalarray which contains the integral part ofthis / divisorat index 0 and the remainderthis % divisorat index 1.BigDecimaldivideToIntegralValue(BigDecimal divisor)Returns a newBigDecimalwhose value is the integral part ofthis / divisor.BigDecimaldivideToIntegralValue(BigDecimal divisor, MathContext mc)Returns a newBigDecimalwhose value is the integral part ofthis / divisor.doubledoubleValue()Returns thisBigDecimalas a double value.booleanequals(Object x)Returnstrueifxis aBigDecimalinstance and if this instance is equal to this big decimal.floatfloatValue()Returns thisBigDecimalas a float value.inthashCode()Returns a hash code for thisBigDecimal.intintValue()Returns thisBigDecimalas an int value.intintValueExact()Returns thisBigDecimalas a int value if it has no fractional part and if its value fits to the int range ([-231..231-1]).longlongValue()Returns thisBigDecimalas an long value.longlongValueExact()Returns thisBigDecimalas a long value if it has no fractional part and if its value fits to the int range ([-263..263-1]).BigDecimalmax(BigDecimal val)Returns the maximum of thisBigDecimalandval.BigDecimalmin(BigDecimal val)Returns the minimum of thisBigDecimalandval.BigDecimalmovePointLeft(int n)Returns a newBigDecimalinstance where the decimal point has been movednplaces to the left.BigDecimalmovePointRight(int n)Returns a newBigDecimalinstance where the decimal point has been movednplaces to the right.BigDecimalmultiply(BigDecimal multiplicand)Returns a newBigDecimalwhose value isthis * multiplicand.BigDecimalmultiply(BigDecimal multiplicand, MathContext mc)Returns a newBigDecimalwhose value isthis * multiplicand.BigDecimalnegate()Returns a newBigDecimalwhose value is the-this.BigDecimalnegate(MathContext mc)Returns a newBigDecimalwhose value is the-this.BigDecimalplus()Returns a newBigDecimalwhose value is+this.BigDecimalplus(MathContext mc)Returns a newBigDecimalwhose value is+this.BigDecimalpow(int n)Returns a newBigDecimalwhose value isthis<sup>n</sup>.BigDecimalpow(int n, MathContext mc)Returns a newBigDecimalwhose value isthis<sup>n</sup>.intprecision()Returns the precision of thisBigDecimal.BigDecimalremainder(BigDecimal divisor)Returns a newBigDecimalwhose value isthis % divisor.BigDecimalremainder(BigDecimal divisor, MathContext mc)Returns a newBigDecimalwhose value isthis % divisor.BigDecimalround(MathContext mc)Returns a newBigDecimalwhose value isthis, rounded according to the passed contextmc.intscale()Returns the scale of thisBigDecimal.BigDecimalscaleByPowerOfTen(int n)Returns a newBigDecimalwhose value isthis * 10<sup>n</sup>.BigDecimalsetScale(int newScale)Returns a newBigDecimalinstance with the specified scale.BigDecimalsetScale(int newScale, int roundingMode)Returns a newBigDecimalinstance with the specified scale.BigDecimalsetScale(int newScale, RoundingMode roundingMode)Returns a newBigDecimalinstance with the specified scale.shortshortValueExact()Returns thisBigDecimalas a short value if it has no fractional part and if its value fits to the short range ([-215..215-1]).intsignum()Returns the sign of thisBigDecimal.BigDecimalstripTrailingZeros()Returns a newBigDecimalinstance with the same value asthisbut with a unscaled value where the trailing zeros have been removed.BigDecimalsubtract(BigDecimal subtrahend)Returns a newBigDecimalwhose value isthis - subtrahend.BigDecimalsubtract(BigDecimal subtrahend, MathContext mc)Returns a newBigDecimalwhose value isthis - subtrahend.BigIntegertoBigInteger()Returns thisBigDecimalas a big integer instance.BigIntegertoBigIntegerExact()Returns thisBigDecimalas a big integer instance if it has no fractional part.StringtoEngineeringString()Returns a string representation of thisBigDecimal.StringtoPlainString()Returns a string representation of thisBigDecimal.StringtoString()Returns a canonical string representation of thisBigDecimal.BigDecimalulp()Returns the unit in the last place (ULP) of thisBigDecimalinstance.BigIntegerunscaledValue()Returns the unscaled value (mantissa) of thisBigDecimalinstance as aBigInteger.static BigDecimalvalueOf(double val)Returns a newBigDecimalinstance whose value is equal toval.static BigDecimalvalueOf(long unscaledVal)Returns a newBigDecimalinstance whose value is equal tounscaledVal.static BigDecimalvalueOf(long unscaledVal, int scale)Returns a newBigDecimalinstance whose value is equal tounscaledVal * 10<sup>-scale</sup>).Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ROUND_UP
public static final int ROUND_UPRounding mode where positive values are rounded towards positive infinity and negative values towards negative infinity.- See Also:
RoundingMode.UP, Constant Field Values
-
ROUND_DOWN
public static final int ROUND_DOWNRounding mode where the values are rounded towards zero.- See Also:
RoundingMode.DOWN, Constant Field Values
-
ROUND_CEILING
public static final int ROUND_CEILINGRounding mode to round towards positive infinity. For positive values this rounding mode behaves asROUND_UP, for negative values asROUND_DOWN.- See Also:
RoundingMode.CEILING, Constant Field Values
-
ROUND_FLOOR
public static final int ROUND_FLOORRounding mode to round towards negative infinity. For positive values this rounding mode behaves asROUND_DOWN, for negative values asROUND_UP.- See Also:
RoundingMode.FLOOR, Constant Field Values
-
ROUND_HALF_UP
public static final int ROUND_HALF_UPRounding mode where values are rounded towards the nearest neighbor. Ties are broken by rounding up.- See Also:
RoundingMode.HALF_UP, Constant Field Values
-
ROUND_HALF_DOWN
public static final int ROUND_HALF_DOWNRounding mode where values are rounded towards the nearest neighbor. Ties are broken by rounding down.- See Also:
RoundingMode.HALF_DOWN, Constant Field Values
-
ROUND_HALF_EVEN
public static final int ROUND_HALF_EVENRounding mode where values are rounded towards the nearest neighbor. Ties are broken by rounding to the even neighbor.- See Also:
RoundingMode.HALF_EVEN, Constant Field Values
-
ROUND_UNNECESSARY
public static final int ROUND_UNNECESSARYRounding mode where the rounding operations throws anArithmeticExceptionfor the case that rounding is necessary, i.e. for the case that the value cannot be represented exactly.- See Also:
RoundingMode.UNNECESSARY, Constant Field Values
-
ZERO
The constant zero as aBigDecimal. -
ONE
The constant one as aBigDecimal. -
TEN
The constant ten as aBigDecimal.
-
-
Constructor Details
-
BigDecimal
public BigDecimal(char[] in, int offset, int len)Constructs a newBigDecimalinstance from a string representation given as a character array.- Parameters:
in- array of characters containing the string representation of thisBigDecimal.offset- first index to be copied.len- number of characters to be used.- Throws:
NumberFormatException- ifoffset < 0 || len <= 0 || offset+len-1 < 0 || offset+len-1 >= in.length, or ifindoes not contain a valid string representation of a big decimal.
-
BigDecimal
Constructs a newBigDecimalinstance from a string representation given as a character array.- Parameters:
in- array of characters containing the string representation of thisBigDecimal.offset- first index to be copied.len- number of characters to be used.mc- rounding mode and precision for the result of this operation.- Throws:
NumberFormatException- ifoffset < 0 || len <= 0 || offset+len-1 < 0 || offset+len-1 >= in.length, or ifindoes not contain a valid string representation of a big decimal.ArithmeticException- ifmc.precision > 0andmc.roundingMode == UNNECESSARYand the new big decimal cannot be represented within the given precision without rounding.
-
BigDecimal
public BigDecimal(char[] in)Constructs a newBigDecimalinstance from a string representation given as a character array.- Parameters:
in- array of characters containing the string representation of thisBigDecimal.- Throws:
NumberFormatException- ifindoes not contain a valid string representation of a big decimal.
-
BigDecimal
Constructs a newBigDecimalinstance from a string representation given as a character array. The result is rounded according to the specified math context.- Parameters:
in- array of characters containing the string representation of thisBigDecimal.mc- rounding mode and precision for the result of this operation.- Throws:
NumberFormatException- ifindoes not contain a valid string representation of a big decimal.ArithmeticException- ifmc.precision > 0andmc.roundingMode == UNNECESSARYand the new big decimal cannot be represented within the given precision without rounding.
-
BigDecimal
Constructs a newBigDecimalinstance from a string representation.- Throws:
NumberFormatException- ifvaldoes not contain a valid string representation of a big decimal.
-
BigDecimal
Constructs a newBigDecimalinstance from a string representation. The result is rounded according to the specified math context.- Parameters:
mc- rounding mode and precision for the result of this operation.- Throws:
NumberFormatException- ifvaldoes not contain a valid string representation of a big decimal.ArithmeticException- ifmc.precision > 0andmc.roundingMode == UNNECESSARYand the new big decimal cannot be represented within the given precision without rounding.
-
BigDecimal
public BigDecimal(double val)Constructs a newBigDecimalinstance from the 64bit doubleval. The constructed big decimal is equivalent to the given double. For example,new BigDecimal(0.1)is equal to0.1000000000000000055511151231257827021181583404541015625. This happens as0.1cannot be represented exactly in binary.To generate a big decimal instance which is equivalent to
0.1use theBigDecimal(String)constructor.- Parameters:
val- double value to be converted to aBigDecimalinstance.- Throws:
NumberFormatException- ifvalis infinity or not a number.
-
BigDecimal
Constructs a newBigDecimalinstance from the 64bit doubleval. The constructed big decimal is equivalent to the given double. For example,new BigDecimal(0.1)is equal to0.1000000000000000055511151231257827021181583404541015625. This happens as0.1cannot be represented exactly in binary.To generate a big decimal instance which is equivalent to
0.1use theBigDecimal(String)constructor.- Parameters:
val- double value to be converted to aBigDecimalinstance.mc- rounding mode and precision for the result of this operation.- Throws:
NumberFormatException- ifvalis infinity or not a number.ArithmeticException- ifmc.precision > 0andmc.roundingMode == UNNECESSARYand the new big decimal cannot be represented within the given precision without rounding.
-
BigDecimal
Constructs a newBigDecimalinstance from the given big integerval. The scale of the result is0. -
BigDecimal
Constructs a newBigDecimalinstance from the given big integerval. The scale of the result is0.- Parameters:
mc- rounding mode and precision for the result of this operation.- Throws:
ArithmeticException- ifmc.precision > 0andmc.roundingMode == UNNECESSARYand the new big decimal cannot be represented within the given precision without rounding.
-
BigDecimal
Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale. The value of this instance isunscaledVal * 10<sup>-scale</sup>).- Throws:
NullPointerException- ifunscaledVal == null.
-
BigDecimal
Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale. The value of this instance is {@code unscaledVal * 10-scale). The result is rounded according to the specified math context. @param mc rounding mode and precision for the result of this operation. @throws ArithmeticException if {@code mc.precision > 0} and {@code mc.roundingMode == UNNECESSARY} and the new big decimal cannot be represented within the given precision without rounding. @throws NullPointerException if {@code unscaledVal == null}. -
BigDecimal
public BigDecimal(int val)Constructs a newBigDecimalinstance from the given intval. The scale of the result is 0.- Parameters:
val- int value to be converted to aBigDecimalinstance.
-
BigDecimal
Constructs a newBigDecimalinstance from the given intval. The scale of the result is0. The result is rounded according to the specified math context.- Parameters:
val- int value to be converted to aBigDecimalinstance.mc- rounding mode and precision for the result of this operation.- Throws:
ArithmeticException- ifmc.precision > 0andc.roundingMode == UNNECESSARYand the new big decimal cannot be represented within the given precision without rounding.
-
BigDecimal
public BigDecimal(long val)Constructs a newBigDecimalinstance from the given longval. The scale of the result is0.- Parameters:
val- long value to be converted to aBigDecimalinstance.
-
BigDecimal
Constructs a newBigDecimalinstance from the given longval. The scale of the result is0. The result is rounded according to the specified math context.- Parameters:
val- long value to be converted to aBigDecimalinstance.mc- rounding mode and precision for the result of this operation.- Throws:
ArithmeticException- ifmc.precision > 0andmc.roundingMode == UNNECESSARYand the new big decimal cannot be represented within the given precision without rounding.
-
-
Method Details
-
valueOf
Returns a newBigDecimalinstance whose value is equal tounscaledVal * 10<sup>-scale</sup>). The scale of the result isscale, and its unscaled value isunscaledVal. -
valueOf
Returns a newBigDecimalinstance whose value is equal tounscaledVal. The scale of the result is0, and its unscaled value isunscaledVal.- Parameters:
unscaledVal- value to be converted to aBigDecimal.- Returns:
BigDecimalinstance with the valueunscaledVal.
-
valueOf
Returns a newBigDecimalinstance whose value is equal toval. The new decimal is constructed as if theBigDecimal(String)constructor is called with an argument which is equal toDouble.toString(val). For example,valueOf("0.1")is converted to (unscaled=1, scale=1), although the double0.1cannot be represented exactly as a double value. In contrast to that, a newBigDecimal(0.1)instance has the value0.1000000000000000055511151231257827021181583404541015625with an unscaled value1000000000000000055511151231257827021181583404541015625and the scale55.- Parameters:
val- double value to be converted to aBigDecimal.- Returns:
BigDecimalinstance with the valueval.- Throws:
NumberFormatException- ifvalis infinite orvalis not a number
-
add
Returns a newBigDecimalwhose value isthis + augend. The scale of the result is the maximum of the scales of the two arguments.- Parameters:
augend- value to be added tothis.- Returns:
this + augend.- Throws:
NullPointerException- ifaugend == null.
-
add
Returns a newBigDecimalwhose value isthis + augend. The result is rounded according to the passed contextmc.- Parameters:
augend- value to be added tothis.mc- rounding mode and precision for the result of this operation.- Returns:
this + augend.- Throws:
NullPointerException- ifaugend == nullormc == null.
-
subtract
Returns a newBigDecimalwhose value isthis - subtrahend. The scale of the result is the maximum of the scales of the two arguments.- Parameters:
subtrahend- value to be subtracted fromthis.- Returns:
this - subtrahend.- Throws:
NullPointerException- ifsubtrahend == null.
-
subtract
Returns a newBigDecimalwhose value isthis - subtrahend. The result is rounded according to the passed contextmc.- Parameters:
subtrahend- value to be subtracted fromthis.mc- rounding mode and precision for the result of this operation.- Returns:
this - subtrahend.- Throws:
NullPointerException- ifsubtrahend == nullormc == null.
-
multiply
Returns a newBigDecimalwhose value isthis * multiplicand. The scale of the result is the sum of the scales of the two arguments.- Parameters:
multiplicand- value to be multiplied withthis.- Returns:
this * multiplicand.- Throws:
NullPointerException- ifmultiplicand == null.
-
multiply
Returns a newBigDecimalwhose value isthis * multiplicand. The result is rounded according to the passed contextmc.- Parameters:
multiplicand- value to be multiplied withthis.mc- rounding mode and precision for the result of this operation.- Returns:
this * multiplicand.- Throws:
NullPointerException- ifmultiplicand == nullormc == null.
-
divide
Returns a newBigDecimalwhose value isthis / divisor. As scale of the result the parameterscaleis used. If rounding is required to meet the specified scale, then the specified rounding moderoundingModeis applied.- Parameters:
divisor- value by whichthisis divided.scale- the scale of the result returned.roundingMode- rounding mode to be used to round the result.- Returns:
this / divisorrounded according to the given rounding mode.- Throws:
NullPointerException- ifdivisor == null.IllegalArgumentException- ifroundingModeis not a valid rounding mode.ArithmeticException- ifdivisor == 0.ArithmeticException- ifroundingMode == ROUND_UNNECESSARYand rounding is necessary according to the given scale.
-
divide
Returns a newBigDecimalwhose value isthis / divisor. As scale of the result the parameterscaleis used. If rounding is required to meet the specified scale, then the specified rounding moderoundingModeis applied.- Parameters:
divisor- value by whichthisis divided.scale- the scale of the result returned.roundingMode- rounding mode to be used to round the result.- Returns:
this / divisorrounded according to the given rounding mode.- Throws:
NullPointerException- ifdivisor == nullorroundingMode == null.ArithmeticException- ifdivisor == 0.ArithmeticException- ifroundingMode == RoundingMode.UNNECESSARY and rounding is necessary according to the given scale and given precision.
-
divide
Returns a newBigDecimalwhose value isthis / divisor. The scale of the result is the scale ofthis. If rounding is required to meet the specified scale, then the specified rounding moderoundingModeis applied.- Parameters:
divisor- value by whichthisis divided.roundingMode- rounding mode to be used to round the result.- Returns:
this / divisorrounded according to the given rounding mode.- Throws:
NullPointerException- ifdivisor == null.IllegalArgumentException- ifroundingModeis not a valid rounding mode.ArithmeticException- ifdivisor == 0.ArithmeticException- ifroundingMode == ROUND_UNNECESSARYand rounding is necessary according to the scale of this.
-
divide
Returns a newBigDecimalwhose value isthis / divisor. The scale of the result is the scale ofthis. If rounding is required to meet the specified scale, then the specified rounding moderoundingModeis applied.- Parameters:
divisor- value by whichthisis divided.roundingMode- rounding mode to be used to round the result.- Returns:
this / divisorrounded according to the given rounding mode.- Throws:
NullPointerException- ifdivisor == nullorroundingMode == null.ArithmeticException- ifdivisor == 0.ArithmeticException- ifroundingMode == RoundingMode.UNNECESSARYand rounding is necessary according to the scale of this.
-
divide
Returns a newBigDecimalwhose value isthis / divisor. The scale of the result is the difference of the scales ofthisanddivisor. If the exact result requires more digits, then the scale is adjusted accordingly. For example,1/128 = 0.0078125which has a scale of7and precision5.- Parameters:
divisor- value by whichthisis divided.- Returns:
this / divisor.- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.ArithmeticException- if the result cannot be represented exactly.
-
divide
Returns a newBigDecimalwhose value isthis / divisor. The result is rounded according to the passed contextmc. If the passed math context specifies precision0, then this call is equivalent tothis.divide(divisor).- Parameters:
divisor- value by whichthisis divided.mc- rounding mode and precision for the result of this operation.- Returns:
this / divisor.- Throws:
NullPointerException- ifdivisor == nullormc == null.ArithmeticException- ifdivisor == 0.ArithmeticException- ifmc.getRoundingMode() == UNNECESSARYand rounding is necessary accordingmc.getPrecision().
-
divideToIntegralValue
Returns a newBigDecimalwhose value is the integral part ofthis / divisor. The quotient is rounded down towards zero to the next integer. For example,0.5/0.2 = 2.- Parameters:
divisor- value by whichthisis divided.- Returns:
- integral part of
this / divisor. - Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.
-
divideToIntegralValue
Returns a newBigDecimalwhose value is the integral part ofthis / divisor. The quotient is rounded down towards zero to the next integer. The rounding mode passed with the parametermcis not considered. But if the precision ofmc > 0and the integral part requires more digits, then anArithmeticExceptionis thrown.- Parameters:
divisor- value by whichthisis divided.mc- math context which determines the maximal precision of the result.- Returns:
- integral part of
this / divisor. - Throws:
NullPointerException- ifdivisor == nullormc == null.ArithmeticException- ifdivisor == 0.ArithmeticException- ifmc.getPrecision() > 0and the result requires more digits to be represented.
-
remainder
Returns a newBigDecimalwhose value isthis % divisor.The remainder is defined as
this - this.divideToIntegralValue(divisor) * divisor.- Parameters:
divisor- value by whichthisis divided.- Returns:
this % divisor.- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.
-
remainder
Returns a newBigDecimalwhose value isthis % divisor.The remainder is defined as
this - this.divideToIntegralValue(divisor) * divisor.The specified rounding mode
mcis used for the division only.- Parameters:
divisor- value by whichthisis divided.mc- rounding mode and precision to be used.- Returns:
this % divisor.- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.ArithmeticException- ifmc.getPrecision() > 0and the result ofthis.divideToIntegralValue(divisor, mc)requires more digits to be represented.
-
divideAndRemainder
Returns aBigDecimalarray which contains the integral part ofthis / divisorat index 0 and the remainderthis % divisorat index 1. The quotient is rounded down towards zero to the next integer.- Parameters:
divisor- value by whichthisis divided.- Returns:
[this.divideToIntegralValue(divisor), this.remainder(divisor)].- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.- See Also:
divideToIntegralValue(java.math.BigDecimal),remainder(java.math.BigDecimal)
-
divideAndRemainder
Returns aBigDecimalarray which contains the integral part ofthis / divisorat index 0 and the remainderthis % divisorat index 1. The quotient is rounded down towards zero to the next integer. The rounding mode passed with the parametermcis not considered. But if the precision ofmc > 0and the integral part requires more digits, then anArithmeticExceptionis thrown.- Parameters:
divisor- value by whichthisis divided.mc- math context which determines the maximal precision of the result.- Returns:
[this.divideToIntegralValue(divisor), this.remainder(divisor)].- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.- See Also:
divideToIntegralValue(java.math.BigDecimal),remainder(java.math.BigDecimal)
-
pow
Returns a newBigDecimalwhose value isthis<sup>n</sup>. The scale of the result isn * this.scale().x.pow(0)returns1, even ifx == 0.Implementation Note: The implementation is based on the ANSI standard X3.274-1996 algorithm.
- Throws:
ArithmeticException- ifn < 0orn > 999999999.
-
pow
Returns a newBigDecimalwhose value isthis<sup>n</sup>. The result is rounded according to the passed contextmc.Implementation Note: The implementation is based on the ANSI standard X3.274-1996 algorithm.
- Parameters:
mc- rounding mode and precision for the result of this operation.- Throws:
ArithmeticException- ifn < 0orn > 999999999.
-
abs
Returns aBigDecimalwhose value is the absolute value ofthis. The scale of the result is the same as the scale of this. -
abs
Returns aBigDecimalwhose value is the absolute value ofthis. The result is rounded according to the passed contextmc. -
negate
Returns a newBigDecimalwhose value is the-this. The scale of the result is the same as the scale of this.- Returns:
-this
-
negate
Returns a newBigDecimalwhose value is the-this. The result is rounded according to the passed contextmc.- Parameters:
mc- rounding mode and precision for the result of this operation.- Returns:
-this
-
plus
Returns a newBigDecimalwhose value is+this. The scale of the result is the same as the scale of this.- Returns:
this
-
plus
Returns a newBigDecimalwhose value is+this. The result is rounded according to the passed contextmc.- Parameters:
mc- rounding mode and precision for the result of this operation.- Returns:
this, rounded
-
signum
public int signum()Returns the sign of thisBigDecimal.- Returns:
-1ifthis < 0,0ifthis == 0,1ifthis > 0.
-
scale
public int scale()Returns the scale of thisBigDecimal. The scale is the number of digits behind the decimal point. The value of thisBigDecimalis theunsignedValue * 10<sup>-scale</sup>. If the scale is negative, then thisBigDecimalrepresents a big integer.- Returns:
- the scale of this
BigDecimal.
-
precision
public int precision()Returns the precision of thisBigDecimal. The precision is the number of decimal digits used to represent this decimal. It is equivalent to the number of digits of the unscaled value. The precision of0is1(independent of the scale).- Returns:
- the precision of this
BigDecimal.
-
unscaledValue
Returns the unscaled value (mantissa) of thisBigDecimalinstance as aBigInteger. The unscaled value can be computed asthis * 10<sup>scale</sup>. -
round
Returns a newBigDecimalwhose value isthis, rounded according to the passed contextmc.If
mc.precision = 0, then no rounding is performed.If
mc.precision > 0andmc.roundingMode == UNNECESSARY, then anArithmeticExceptionis thrown if the result cannot be represented exactly within the given precision.- Parameters:
mc- rounding mode and precision for the result of this operation.- Returns:
thisrounded according to the passed context.- Throws:
ArithmeticException- ifmc.precision > 0andmc.roundingMode == UNNECESSARYand this cannot be represented within the given precision.
-
setScale
Returns a newBigDecimalinstance with the specified scale.If the new scale is greater than the old scale, then additional zeros are added to the unscaled value. In this case no rounding is necessary.
If the new scale is smaller than the old scale, then trailing digits are removed. If these trailing digits are not zero, then the remaining unscaled value has to be rounded. For this rounding operation the specified rounding mode is used.
- Parameters:
newScale- scale of the result returned.roundingMode- rounding mode to be used to round the result.- Returns:
- a new
BigDecimalinstance with the specified scale. - Throws:
NullPointerException- ifroundingMode == null.ArithmeticException- ifroundingMode == ROUND_UNNECESSARYand rounding is necessary according to the given scale.
-
setScale
Returns a newBigDecimalinstance with the specified scale.If the new scale is greater than the old scale, then additional zeros are added to the unscaled value. In this case no rounding is necessary.
If the new scale is smaller than the old scale, then trailing digits are removed. If these trailing digits are not zero, then the remaining unscaled value has to be rounded. For this rounding operation the specified rounding mode is used.
- Parameters:
newScale- scale of the result returned.roundingMode- rounding mode to be used to round the result.- Returns:
- a new
BigDecimalinstance with the specified scale. - Throws:
IllegalArgumentException- ifroundingModeis not a valid rounding mode.ArithmeticException- ifroundingMode == ROUND_UNNECESSARYand rounding is necessary according to the given scale.
-
setScale
Returns a newBigDecimalinstance with the specified scale. If the new scale is greater than the old scale, then additional zeros are added to the unscaled value. If the new scale is smaller than the old scale, then trailing zeros are removed. If the trailing digits are not zeros then an ArithmeticException is thrown.If no exception is thrown, then the following equation holds:
x.setScale(s).compareTo(x) == 0.- Parameters:
newScale- scale of the result returned.- Returns:
- a new
BigDecimalinstance with the specified scale. - Throws:
ArithmeticException- if rounding would be necessary.
-
movePointLeft
Returns a newBigDecimalinstance where the decimal point has been movednplaces to the left. Ifn < 0then the decimal point is moved-nplaces to the right.The result is obtained by changing its scale. If the scale of the result becomes negative, then its precision is increased such that the scale is zero.
Note, that
movePointLeft(0)returns a result which is mathematically equivalent, but which hasscale >= 0. -
movePointRight
Returns a newBigDecimalinstance where the decimal point has been movednplaces to the right. Ifn < 0then the decimal point is moved-nplaces to the left.The result is obtained by changing its scale. If the scale of the result becomes negative, then its precision is increased such that the scale is zero.
Note, that
movePointRight(0)returns a result which is mathematically equivalent, but which has scale >= 0. -
scaleByPowerOfTen
Returns a newBigDecimalwhose value isthis * 10<sup>n</sup>. The scale of the result isthis.scale()-n. The precision of the result is the precision ofthis.This method has the same effect as
movePointRight(int), except that the precision is not changed. -
stripTrailingZeros
Returns a newBigDecimalinstance with the same value asthisbut with a unscaled value where the trailing zeros have been removed. If the unscaled value ofthishas n trailing zeros, then the scale and the precision of the result has been reduced by n.- Returns:
- a new
BigDecimalinstance equivalent to this where the trailing zeros of the unscaled value have been removed.
-
compareTo
Compares thisBigDecimalwithval. Returns one of the three values1,0, or-1. The method behaves as ifthis.subtract(val)is computed. If this difference is > 0 then 1 is returned, if the difference is < 0 then -1 is returned, and if the difference is 0 then 0 is returned. This means, that if two decimal instances are compared which are equal in value but differ in scale, then these two instances are considered as equal.- Specified by:
compareToin interfaceComparable<BigDecimal>- Parameters:
val- value to be compared withthis.- Returns:
1ifthis > val,-1ifthis < val,0ifthis == val.- Throws:
NullPointerException- ifval == null.
-
equals
Returnstrueifxis aBigDecimalinstance and if this instance is equal to this big decimal. Two big decimals are equal if their unscaled value and their scale is equal. For example, 1.0 (10*10-1) is not equal to 1.00 (100*10-2). Similarly, zero instances are not equal if their scale differs.- Overrides:
equalsin classObject- Parameters:
x- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
min
Returns the minimum of thisBigDecimalandval.- Parameters:
val- value to be used to compute the minimum with this.- Returns:
min(this, val.- Throws:
NullPointerException- ifval == null.
-
max
Returns the maximum of thisBigDecimalandval.- Parameters:
val- value to be used to compute the maximum with this.- Returns:
max(this, val.- Throws:
NullPointerException- ifval == null.
-
hashCode
public int hashCode()Returns a hash code for thisBigDecimal.- Overrides:
hashCodein classObject- Returns:
- hash code for
this. - See Also:
Object.equals(java.lang.Object)
-
toString
Returns a canonical string representation of thisBigDecimal. If necessary, scientific notation is used. This representation always prints all significant digits of this value.If the scale is negative or if
scale - precision >= 6then scientific notation is used. -
toEngineeringString
Returns a string representation of thisBigDecimal. This representation always prints all significant digits of this value.If the scale is negative or if
scale - precision >= 6then engineering notation is used. Engineering notation is similar to the scientific notation except that the exponent is made to be a multiple of 3 such that the integer part is >= 1 and < 1000.- Returns:
- a string representation of
thisin engineering notation if necessary.
-
toPlainString
Returns a string representation of thisBigDecimal. No scientific notation is used. This methods adds zeros where necessary.If this string representation is used to create a new instance, this instance is generally not identical to
thisas the precision changes.x.equals(new BigDecimal(x.toPlainString())usually returnsfalse.x.compareTo(new BigDecimal(x.toPlainString())returns0.- Returns:
- a string representation of
thiswithout exponent part.
-
toBigInteger
Returns thisBigDecimalas a big integer instance. A fractional part is discarded.- Returns:
- this
BigDecimalas a big integer instance.
-
toBigIntegerExact
Returns thisBigDecimalas a big integer instance if it has no fractional part. If thisBigDecimalhas a fractional part, i.e. if rounding would be necessary, anArithmeticExceptionis thrown.- Returns:
- this
BigDecimalas a big integer value. - Throws:
ArithmeticException- if rounding is necessary.
-
longValue
public long longValue()Returns thisBigDecimalas an long value. Any fractional part is discarded. If the integral part ofthisis too big to be represented as an long, thenthis % 2<sup>64</sup>is returned. -
longValueExact
public long longValueExact()Returns thisBigDecimalas a long value if it has no fractional part and if its value fits to the int range ([-263..263-1]). If these conditions are not met, anArithmeticExceptionis thrown.- Throws:
ArithmeticException- if rounding is necessary or the number doesn't fit in a long.
-
intValue
public int intValue()Returns thisBigDecimalas an int value. Any fractional part is discarded. If the integral part ofthisis too big to be represented as an int, thenthis % 2<sup>32</sup>is returned. -
intValueExact
public int intValueExact()Returns thisBigDecimalas a int value if it has no fractional part and if its value fits to the int range ([-231..231-1]). If these conditions are not met, anArithmeticExceptionis thrown.- Throws:
ArithmeticException- if rounding is necessary or the number doesn't fit in an int.
-
shortValueExact
public short shortValueExact()Returns thisBigDecimalas a short value if it has no fractional part and if its value fits to the short range ([-215..215-1]). If these conditions are not met, anArithmeticExceptionis thrown.- Throws:
ArithmeticException- if rounding is necessary of the number doesn't fit in a short.
-
byteValueExact
public byte byteValueExact()Returns thisBigDecimalas a byte value if it has no fractional part and if its value fits to the byte range ([-128..127]). If these conditions are not met, anArithmeticExceptionis thrown.- Throws:
ArithmeticException- if rounding is necessary or the number doesn't fit in a byte.
-
floatValue
public float floatValue()Returns thisBigDecimalas a float value. Ifthisis too big to be represented as an float, thenFloat.POSITIVE_INFINITYorFloat.NEGATIVE_INFINITYis returned.Note, that if the unscaled value has more than 24 significant digits, then this decimal cannot be represented exactly in a float variable. In this case the result is rounded.
For example, if the instance
x1 = new BigDecimal("0.1")cannot be represented exactly as a float, and thusx1.equals(new BigDecimal(x1.floatValue())returnsfalsefor this case.Similarly, if the instance
new BigDecimal(16777217)is converted to a float, the result is1.6777216E7.- Specified by:
floatValuein classNumber- Returns:
- this
BigDecimalas a float value.
-
doubleValue
public double doubleValue()Returns thisBigDecimalas a double value. Ifthisis too big to be represented as an float, thenDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYis returned.Note, that if the unscaled value has more than 53 significant digits, then this decimal cannot be represented exactly in a double variable. In this case the result is rounded.
For example, if the instance
x1 = new BigDecimal("0.1")cannot be represented exactly as a double, and thusx1.equals(new BigDecimal(x1.doubleValue())returnsfalsefor this case.Similarly, if the instance
new BigDecimal(9007199254740993L)is converted to a double, the result is9.007199254740992E15.- Specified by:
doubleValuein classNumber- Returns:
- this
BigDecimalas a double value.
-
ulp
Returns the unit in the last place (ULP) of thisBigDecimalinstance. An ULP is the distance to the nearest big decimal with the same precision.The amount of a rounding error in the evaluation of a floating-point operation is often expressed in ULPs. An error of 1 ULP is often seen as a tolerable error.
For class
BigDecimal, the ULP of a number is simply 10-scale. For example,new BigDecimal(0.1).ulp()returns1E-55.- Returns:
- unit in the last place (ULP) of this
BigDecimalinstance.
-