Class TBigDecimal
- All Implemented Interfaces:
Serializable,Comparable<TBigDecimal>
BigDecimal instance is represented with a unscaled arbitrary
precision mantissa (the unscaled value) and a scale. The value of the
BigDecimal is unscaledValue 10^(-scale).- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TBigDecimalThe constant one as aBigDecimal.static final intRounding mode to round towards positive infinity.static final intRounding mode where the values are rounded towards zero.static final intRounding mode to round towards negative infinity.static final intRounding mode where values are rounded towards the nearest neighbor.static final intRounding mode where values are rounded towards the nearest neighbor.static final intRounding mode where values are rounded towards the nearest neighbor.static final intRounding 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.static final intRounding mode where positive values are rounded towards positive infinity and negative values towards negative infinity.static final TBigDecimalThe constant ten as aBigDecimal.static final TBigDecimalThe constant zero as aBigDecimal. -
Constructor Summary
ConstructorsConstructorDescriptionTBigDecimal(char[] in) Constructs a newBigDecimalinstance from a string representation given as a character array.TBigDecimal(char[] in, int offset, int len) Constructs a newBigDecimalinstance from a string representation given as a character array.TBigDecimal(char[] in, int offset, int len, TMathContext mc) Constructs a newBigDecimalinstance from a string representation given as a character array.TBigDecimal(char[] in, TMathContext mc) Constructs a newBigDecimalinstance from a string representation given as a character array.TBigDecimal(double val) Constructs a newBigDecimalinstance from the 64bit doubleval.TBigDecimal(double val, TMathContext mc) Constructs a newBigDecimalinstance from the 64bit doubleval.TBigDecimal(int val) Constructs a newBigDecimalinstance from the given intval.TBigDecimal(int val, TMathContext mc) Constructs a newBigDecimalinstance from the given intval.TBigDecimal(long val) Constructs a newBigDecimalinstance from the given longval.TBigDecimal(long val, TMathContext mc) Constructs a newBigDecimalinstance from the given longval.TBigDecimal(String val) Constructs a newBigDecimalinstance from a string representation.TBigDecimal(String val, TMathContext mc) Constructs a newBigDecimalinstance from a string representation.TBigDecimal(TBigInteger val) Constructs a newBigDecimalinstance from the given big integerval.TBigDecimal(TBigInteger unscaledVal, int scale) Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale.TBigDecimal(TBigInteger unscaledVal, int scale, TMathContext mc) Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale.TBigDecimal(TBigInteger val, TMathContext mc) Constructs a newBigDecimalinstance from the given big integerval. -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns a newBigDecimalwhose value is the absolute value ofthis.abs(TMathContext mc) Returns a newBigDecimalwhose value is the absolute value ofthis.add(TBigDecimal augend) Returns a newBigDecimalwhose value isthis + augend.add(TBigDecimal augend, TMathContext mc) Returns a newBigDecimalwhose value isthis + augend.byteReturns thisBigDecimalas a byte value if it has no fractional part and if its value fits to the byte range ([-128..127]).intcompareTo(TBigDecimal val) Compares thisBigDecimalwithval.divide(TBigDecimal divisor) Returns a newBigDecimalwhose value isthis / divisor.divide(TBigDecimal divisor, int roundingMode) Returns a newBigDecimalwhose value isthis / divisor.divide(TBigDecimal divisor, int scale, int roundingMode) Returns a newBigDecimalwhose value isthis / divisor.divide(TBigDecimal divisor, int scale, TRoundingMode roundingMode) Returns a newBigDecimalwhose value isthis / divisor.divide(TBigDecimal divisor, TMathContext mc) Returns a newBigDecimalwhose value isthis / divisor.divide(TBigDecimal divisor, TRoundingMode roundingMode) Returns a newBigDecimalwhose value isthis / divisor.divideAndRemainder(TBigDecimal divisor) Returns aBigDecimalarray which contains the integral part ofthis / divisorat index 0 and the remainderthis % divisorat index 1.divideAndRemainder(TBigDecimal divisor, TMathContext mc) Returns aBigDecimalarray which contains the integral part ofthis / divisorat index 0 and the remainderthis % divisorat index 1.divideToIntegralValue(TBigDecimal divisor) Returns a newBigDecimalwhose value is the integral part ofthis / divisor.divideToIntegralValue(TBigDecimal divisor, TMathContext mc) Returns a newBigDecimalwhose value is the integral part ofthis / divisor.doubleReturns thisBigDecimalas a double value.booleanReturnstrueifxis aBigDecimalinstance and if this instance is equal to this big decimal.floatReturns thisBigDecimalas a float value.inthashCode()Returns a hash code for thisBigDecimal.intintValue()Returns thisBigDecimalas an int value.intReturns thisBigDecimalas a int value if it has no fractional part and if its value fits to the int range ([-2^{31}..2^{31}-1]).longReturns thisBigDecimalas an long value.longReturns thisBigDecimalas a long value if it has no fractional part and if its value fits to the int range ([-2^{63}..2^{63}-1]).max(TBigDecimal val) Returns the maximum of thisBigDecimalandval.min(TBigDecimal val) Returns the minimum of thisBigDecimalandval.movePointLeft(int n) Returns a newBigDecimalinstance where the decimal point has been movednplaces to the left.movePointRight(int n) Returns a newBigDecimalinstance where the decimal point has been movednplaces to the right.multiply(TBigDecimal multiplicand) Returns a newBigDecimalwhose value isthis * multiplicand.multiply(TBigDecimal multiplicand, TMathContext mc) Returns a newBigDecimalwhose value isthis * multiplicand.negate()Returns a newBigDecimalwhose value is the-this.negate(TMathContext mc) Returns a newBigDecimalwhose value is the-this.plus()Returns a newBigDecimalwhose value is+this.plus(TMathContext mc) Returns a newBigDecimalwhose value is+this.pow(int n) Returns a newBigDecimalwhose value isthis ^ n.pow(int n, TMathContext mc) Returns a newBigDecimalwhose value isthis ^ n.intReturns the precision of thisBigDecimal.remainder(TBigDecimal divisor) Returns a newBigDecimalwhose value isthis % divisor.remainder(TBigDecimal divisor, TMathContext mc) Returns a newBigDecimalwhose value isthis % divisor.round(TMathContext mc) Returns a newBigDecimalwhose value isthis, rounded according to the passed contextmc.intscale()Returns the scale of thisBigDecimal.scaleByPowerOfTen(int n) Returns a newBigDecimalwhose value isthis10^n.setScale(int newScale) Returns a newBigDecimalinstance with the specified scale.setScale(int newScale, int roundingMode) Returns a newBigDecimalinstance with the specified scale.setScale(int newScale, TRoundingMode roundingMode) Returns a newBigDecimalinstance with the specified scale.shortReturns thisBigDecimalas a short value if it has no fractional part and if its value fits to the short range ([-2^{15}..2^{15}-1]).intsignum()Returns the sign of thisBigDecimal.Returns a newBigDecimalinstance with the same value asthisbut with a unscaled value where the trailing zeros have been removed.subtract(TBigDecimal subtrahend) Returns a newBigDecimalwhose value isthis - subtrahend.subtract(TBigDecimal subtrahend, TMathContext mc) Returns a newBigDecimalwhose value isthis - subtrahend.Returns thisBigDecimalas a big integer instance.Returns thisBigDecimalas a big integer instance if it has no fractional part.Returns a string representation of thisBigDecimal.Returns a string representation of thisBigDecimal.toString()Returns a canonical string representation of thisBigDecimal.ulp()Returns the unit in the last place (ULP) of thisBigDecimalinstance.Returns the unscaled value (mantissa) of thisBigDecimalinstance as aBigInteger.static TBigDecimalvalueOf(double val) Returns a newBigDecimalinstance whose value is equal toval.static TBigDecimalvalueOf(long unscaledVal) Returns a newBigDecimalinstance whose value is equal tounscaledVal.static TBigDecimalvalueOf(long unscaledVal, int scale) Returns a newBigDecimalinstance whose value is equal tounscaledVal10^(-scale).Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
The constant zero as aBigDecimal. -
ONE
The constant one as aBigDecimal. -
TEN
The constant ten as aBigDecimal. -
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:
-
ROUND_DOWN
public static final int ROUND_DOWNRounding mode where the values are rounded towards zero.- See Also:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
-
Constructor Details
-
TBigDecimal
public TBigDecimal(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:
NullPointerException- ifin == null.NumberFormatException- ifoffset < 0orlen <= 0oroffset+len-1 < 0oroffset+len-1 >= in.length.NumberFormatException- if in does not contain a valid string representation of a big decimal.
-
TBigDecimal
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:
NullPointerException- ifin == null.NumberFormatException- ifoffset < 0orlen <= 0oroffset+len-1 < 0oroffset+len-1 >= in.length.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.
-
TBigDecimal
public TBigDecimal(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:
NullPointerException- ifin == null.NumberFormatException- ifindoes not contain a valid string representation of a big decimal.
-
TBigDecimal
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:
NullPointerException- ifin == null.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.
-
TBigDecimal
Constructs a newBigDecimalinstance from a string representation.- Parameters:
val- string containing the string representation of thisBigDecimal.- Throws:
NumberFormatException- ifvaldoes not contain a valid string representation of a big decimal.
-
TBigDecimal
Constructs a newBigDecimalinstance from a string representation. The result is rounded according to the specified math context.- Parameters:
val- string containing the string representation of thisBigDecimal.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.
-
TBigDecimal
public TBigDecimal(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.
-
TBigDecimal
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.
-
TBigDecimal
Constructs a newBigDecimalinstance from the given big integerval. The scale of the result is0.- Parameters:
val-BigIntegervalue to be converted to aBigDecimalinstance.
-
TBigDecimal
Constructs a newBigDecimalinstance from the given big integerval. The scale of the result is0.- Parameters:
val-BigIntegervalue 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.
-
TBigDecimal
Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale. The value of this instance isunscaledVal10^(-scale).- Parameters:
unscaledVal-BigIntegerrepresenting the unscaled value of thisBigDecimalinstance.scale- scale of thisBigDecimalinstance.- Throws:
NullPointerException- ifunscaledVal == null.
-
TBigDecimal
Constructs a newBigDecimalinstance from a given unscaled valueunscaledValand a given scale. The value of this instance isunscaledVal10^(-scale). The result is rounded according to the specified math context.- Parameters:
unscaledVal-BigIntegerrepresenting the unscaled value of thisBigDecimalinstance.scale- scale of thisBigDecimalinstance.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.NullPointerException- ifunscaledVal == null.
-
TBigDecimal
public TBigDecimal(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.
-
TBigDecimal
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.
-
TBigDecimal
public TBigDecimal(long val) Constructs a newBigDecimalinstance from the given longval. The scale of the result is0.- Parameters:
val- long value to be converted to aBigDecimalinstance.
-
TBigDecimal
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 tounscaledVal10^(-scale). The scale of the result isscale, and its unscaled value isunscaledVal.- Parameters:
unscaledVal- unscaled value to be used to construct the newBigDecimal.scale- scale to be used to construct the newBigDecimal.- Returns:
BigDecimalinstance with the valueunscaledVal* 10^(-unscaledVal).
-
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:
-
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:
-
pow
Returns a newBigDecimalwhose value isthis ^ n. The scale of the result isntimes the scales ofthis.x.pow(0)returns1, even ifx == 0.Implementation Note: The implementation is based on the ANSI standard X3.274-1996 algorithm.
- Parameters:
n- exponent to whichthisis raised.- Returns:
this ^ n.- Throws:
ArithmeticException- ifn < 0orn > 999999999.
-
pow
Returns a newBigDecimalwhose value isthis ^ n. The result is rounded according to the passed contextmc.Implementation Note: The implementation is based on the ANSI standard X3.274-1996 algorithm.
- Parameters:
n- exponent to whichthisis raised.mc- rounding mode and precision for the result of this operation.- Returns:
this ^ n.- Throws:
ArithmeticException- ifn < 0orn > 999999999.
-
abs
Returns a newBigDecimalwhose value is the absolute value ofthis. The scale of the result is the same as the scale of this.- Returns:
abs(this)
-
abs
Returns a newBigDecimalwhose value is the absolute value ofthis. The result is rounded according to the passed contextmc.- Parameters:
mc- rounding mode and precision for the result of this operation.- Returns:
abs(this)
-
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 the unsignedValue * 10^(-scale). 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 asthis10^(scale).- Returns:
- unscaled value (this * 10^(scale)).
-
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.- Parameters:
n- number of placed the decimal point has to be moved.- Returns:
this * 10^(-n).
-
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.- Parameters:
n- number of placed the decimal point has to be moved.- Returns:
this * 10^n.
-
scaleByPowerOfTen
Returns a newBigDecimalwhose value isthis10^n. 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.- Parameters:
n- number of places the decimal point has to be moved.- Returns:
this * 10^n
-
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 invalid input: '<' 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<TBigDecimal>- 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. -
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. -
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 invalid input: '<' 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^64 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 ([-2^{63}..2^{63}-1]). If these conditions are not met, anArithmeticExceptionis thrown.- Returns:
- this
BigDecimalas a long value. - 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^32 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 ([-2^{31}..2^{31}-1]). If these conditions are not met, anArithmeticExceptionis thrown.- Returns:
- this
BigDecimalas a int value. - Throws:
ArithmeticException- if rounding is necessary or the number doesn't fit in a 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 ([-2^{15}..2^{15}-1]). If these conditions are not met, anArithmeticExceptionis thrown.- Returns:
- this
BigDecimalas a short value. - 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.- Returns:
- this
BigDecimalas a byte value. - 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.folatValue())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.
-