Class BigDecimal
- java.lang.Object
-
- java.lang.Number
-
- com.ibm.icu.math.BigDecimal
-
- All Implemented Interfaces:
Serializable,Comparable<BigDecimal>
public class BigDecimal extends Number implements Serializable, Comparable<BigDecimal>
TheBigDecimalclass implements immutable arbitrary-precision decimal numbers. The methods of theBigDecimalclass provide operations for fixed and floating point arithmetic, comparison, format conversions, and hashing.As the numbers are decimal, there is an exact correspondence between an instance of a
BigDecimalobject and itsStringrepresentation; theBigDecimalclass provides direct conversions to and fromStringand character array (char[]) objects, as well as conversions to and from the Java primitive types (which may not be exact) andBigInteger.In the descriptions of constructors and methods in this documentation, the value of a
BigDecimalnumber object is shown as the result of invoking thetoString()method on the object. The internal representation of a decimal number is neither defined nor exposed, and is not permitted to affect the result of any operation.The floating point arithmetic provided by this class is defined by the ANSI X3.274-1996 standard, and is also documented at
http://www2.hursley.ibm.com/decimal
[This URL will change.]Operator methods
Operations on
BigDecimalnumbers are controlled by aMathContextobject, which provides the context (precision and other information) for the operation. Methods that can take aMathContextparameter implement the standard arithmetic operators forBigDecimalobjects and are known as operator methods. The default settings provided by the constantMathContext.DEFAULT(digits=9, form=SCIENTIFIC, lostDigits=false, roundingMode=ROUND_HALF_UP) perform general-purpose floating point arithmetic to nine digits of precision. TheMathContextparameter must not benull.Each operator method also has a version provided which does not take a
MathContextparameter. For this version of each method, the context settings used aredigits=0, form=PLAIN, lostDigits=false, roundingMode=ROUND_HALF_UP; these settings perform fixed point arithmetic with unlimited precision, as defined for the original BigDecimal class in Java 1.1 and Java 1.2.For monadic operators, only the optional
MathContextparameter is present; the operation acts upon the current object.For dyadic operators, a
BigDecimalparameter is always present; it must not benull. The operation acts with the current object being the left-hand operand and theBigDecimalparameter being the right-hand operand.For example, adding two
BigDecimalobjects referred to by the namesawardandextracould be written as any of:award.add(extra)
award.add(extra, MathContext.DEFAULT)
award.add(extra, acontext)(where
acontextis aMathContextobject), which would return aBigDecimalobject whose value is the result of addingawardandextraunder the appropriate context settings.When a
BigDecimaloperator method is used, a set of rules define what the result will be (and, by implication, how the result would be represented as a character string). These rules are defined in the BigDecimal arithmetic documentation (see the URL above), but in summary:- Results are normally calculated with up to some maximum number of significant digits. For example, if the
MathContextparameter for an operation wereMathContext.DEFAULTthen the result would be rounded to 9 digits; the division of 2 by 3 would then result in 0.666666667.
You can change the default of 9 significant digits by providing the method with a suitableMathContextobject. This lets you calculate using as many digits as you need -- thousands, if necessary. Fixed point (scaled) arithmetic is indicated by using adigitssetting of 0 (or omitting theMathContextparameter).
Similarly, you can change the algorithm used for rounding from the default "classic" algorithm. -
In standard arithmetic (that is, when the
formsetting is notPLAIN), a zero result is always expressed as the single digit'0'(that is, with no sign, decimal point, or exponent part). -
Except for the division and power operators in standard arithmetic, trailing zeros are preserved (this is in contrast
to binary floating point operations and most electronic calculators, which lose the information about trailing zeros
in the fractional part of results).
So, for example:new BigDecimal("2.40").add( new BigDecimal("2")) => "4.40"
new BigDecimal("2.40").subtract(new BigDecimal("2")) => "0.40"
new BigDecimal("2.40").multiply(new BigDecimal("2")) => "4.80"
new BigDecimal("2.40").divide( new BigDecimal("2"), def) => "1.2"where the value on the right of the
=>would be the result of the operation, expressed as aString, anddef(in this and following examples) refers toMathContext.DEFAULT). This preservation of trailing zeros is desirable for most calculations (including financial calculations). If necessary, trailing zeros may be easily removed using division by 1. -
In standard arithmetic, exponential form is used for a result depending on its value and the current setting of
digits(the default is 9 digits). If the number of places needed before the decimal point exceeds thedigitssetting, or the absolute value of the number is less than0.000001, then the number will be expressed in exponential notation; thusnew BigDecimal("1e+6").multiply(new BigDecimal("1e+6"), def)results in
1E+12instead of1000000000000, andnew BigDecimal("1").divide(new BigDecimal("3E+10"), def)results in
3.33333333E-11instead of0.0000000000333333333.The form of the exponential notation (scientific or engineering) is determined by the
formsetting.
The names of methods in this class follow the conventions established by
java.lang.Number,java.math.BigInteger, andjava.math.BigDecimalin Java 1.1 and Java 1.2.- Author:
- Mike Cowlishaw
- See Also:
MathContext, Serialized Form- Status:
- Stable ICU 2.0.
-
-
Field Summary
Fields Modifier and Type Field Description static BigDecimalONETheBigDecimalconstant "1".static intROUND_CEILINGRounding mode to round to a more positive number.static intROUND_DOWNRounding mode to round towards zero.static intROUND_FLOORRounding mode to round to a more negative number.static intROUND_HALF_DOWNRounding mode to round to nearest neighbor, where an equidistant value is rounded down.static intROUND_HALF_EVENRounding mode to round to nearest neighbor, where an equidistant value is rounded to the nearest even neighbor.static intROUND_HALF_UPRounding mode to round to nearest neighbor, where an equidistant value is rounded up.static intROUND_UNNECESSARYRounding mode to assert that no rounding is necessary.static intROUND_UPRounding mode to round away from zero.static BigDecimalTENTheBigDecimalconstant "10".static BigDecimalZEROTheBigDecimalconstant "0".
-
Constructor Summary
Constructors Constructor Description BigDecimal(char[] inchars)Constructs aBigDecimalobject from an array of characters.BigDecimal(char[] inchars, int offset, int length)Constructs aBigDecimalobject from an array of characters.BigDecimal(double num)Constructs aBigDecimalobject directly from adouble.BigDecimal(int num)Constructs aBigDecimalobject directly from aint.BigDecimal(long num)Constructs aBigDecimalobject directly from along.BigDecimal(String string)Constructs aBigDecimalobject from aString.BigDecimal(BigDecimal bd)Constructs aBigDecimalobject from ajava.math.BigDecimal.BigDecimal(BigInteger bi)Constructs aBigDecimalobject from aBigInteger, with scale 0.BigDecimal(BigInteger bi, int scale)Constructs aBigDecimalobject from aBigIntegerand a scale.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigDecimalabs()Returns a plainBigDecimalwhose value is the absolute value of thisBigDecimal.BigDecimalabs(MathContext set)Returns aBigDecimalwhose value is the absolute value of thisBigDecimal.BigDecimaladd(BigDecimal rhs)Returns a plainBigDecimalwhose value isthis+rhs, using fixed point arithmetic.BigDecimaladd(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value isthis+rhs.bytebyteValueExact()Converts thisBigDecimalto abyte.intcompareTo(BigDecimal rhs)Compares thisBigDecimalto another, using unlimited precision.intcompareTo(BigDecimal rhs, MathContext set)Compares thisBigDecimalto another.BigDecimaldivide(BigDecimal rhs)Returns a plainBigDecimalwhose value isthis/rhs, using fixed point arithmetic.BigDecimaldivide(BigDecimal rhs, int round)Returns a plainBigDecimalwhose value isthis/rhs, using fixed point arithmetic and a rounding mode.BigDecimaldivide(BigDecimal rhs, int scale, int round)Returns a plainBigDecimalwhose value isthis/rhs, using fixed point arithmetic and a given scale and rounding mode.BigDecimaldivide(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value isthis/rhs.BigDecimaldivideInteger(BigDecimal rhs)Returns a plainBigDecimalwhose value is the integer part ofthis/rhs.BigDecimaldivideInteger(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value is the integer part ofthis/rhs.doubledoubleValue()Converts thisBigDecimalto adouble.booleanequals(Object obj)Compares thisBigDecimalwithrhsfor equality.floatfloatValue()Converts thisBigDecimalto afloat.Stringformat(int before, int after)Returns theStringrepresentation of thisBigDecimal, modified by layout parameters.Stringformat(int before, int after, int explaces, int exdigits, int exformint, int exround)Returns theStringrepresentation of thisBigDecimal, modified by layout parameters and allowing exponential notation.inthashCode()Returns the hashcode for thisBigDecimal.intintValue()Converts thisBigDecimalto anint.intintValueExact()Converts thisBigDecimalto anint.longlongValue()Converts thisBigDecimalto along.longlongValueExact()Converts thisBigDecimalto along.BigDecimalmax(BigDecimal rhs)Returns a plainBigDecimalwhose value is the maximum ofthisandrhs.BigDecimalmax(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value is the maximum ofthisandrhs.BigDecimalmin(BigDecimal rhs)Returns a plainBigDecimalwhose value is the minimum ofthisandrhs.BigDecimalmin(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value is the minimum ofthisandrhs.BigDecimalmovePointLeft(int n)Returns a plainBigDecimalwhose decimal point has been moved to the left by a specified number of positions.BigDecimalmovePointRight(int n)Returns a plainBigDecimalwhose decimal point has been moved to the right by a specified number of positions.BigDecimalmultiply(BigDecimal rhs)Returns a plainBigDecimalwhose value isthis*rhs, using fixed point arithmetic.BigDecimalmultiply(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value isthis*rhs.BigDecimalnegate()Returns a plainBigDecimalwhose value is-this.BigDecimalnegate(MathContext set)Returns aBigDecimalwhose value is-this.BigDecimalplus()Returns a plainBigDecimalwhose value is+this.BigDecimalplus(MathContext set)Returns aBigDecimalwhose value is+this.BigDecimalpow(BigDecimal rhs)Returns a plainBigDecimalwhose value isthis**rhs, using fixed point arithmetic.BigDecimalpow(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value isthis**rhs.BigDecimalremainder(BigDecimal rhs)Returns a plainBigDecimalwhose value is the remainder ofthis/rhs, using fixed point arithmetic.BigDecimalremainder(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value is the remainder ofthis/rhs.intscale()Returns the scale of thisBigDecimal.BigDecimalsetScale(int scale)Returns a plainBigDecimalwith a given scale.BigDecimalsetScale(int scale, int round)Returns a plainBigDecimalwith a given scale.shortshortValueExact()Converts thisBigDecimalto ashort.intsignum()Returns the sign of thisBigDecimal, as anint.BigDecimalsubtract(BigDecimal rhs)Returns a plainBigDecimalwhose value isthis-rhs, using fixed point arithmetic.BigDecimalsubtract(BigDecimal rhs, MathContext set)Returns aBigDecimalwhose value isthis-rhs.BigDecimaltoBigDecimal()Converts thisBigDecimalto ajava.math.BigDecimal.BigIntegertoBigInteger()Converts thisBigDecimalto ajava.math.BigInteger.BigIntegertoBigIntegerExact()Converts thisBigDecimalto ajava.math.BigInteger.char[]toCharArray()Returns theBigDecimalas a character array.StringtoString()Returns theBigDecimalas aString.BigIntegerunscaledValue()Returns the number as aBigIntegerafter removing the scale.static BigDecimalvalueOf(double dub)Translates adoubleto aBigDecimal.static BigDecimalvalueOf(long lint)Translates alongto aBigDecimal.static BigDecimalvalueOf(long lint, int scale)Translates alongto aBigDecimalwith a given scale.-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Field Detail
-
ZERO
public static final BigDecimal ZERO
TheBigDecimalconstant "0".
-
ONE
public static final BigDecimal ONE
TheBigDecimalconstant "1".
-
TEN
public static final BigDecimal TEN
TheBigDecimalconstant "10".
-
ROUND_CEILING
public static final int ROUND_CEILING
Rounding mode to round to a more positive number.- See Also:
MathContext.ROUND_CEILING, Constant Field Values- Status:
- Stable ICU 2.0.
-
ROUND_DOWN
public static final int ROUND_DOWN
Rounding mode to round towards zero.- See Also:
MathContext.ROUND_DOWN, Constant Field Values- Status:
- Stable ICU 2.0.
-
ROUND_FLOOR
public static final int ROUND_FLOOR
Rounding mode to round to a more negative number.- See Also:
MathContext.ROUND_FLOOR, Constant Field Values- Status:
- Stable ICU 2.0.
-
ROUND_HALF_DOWN
public static final int ROUND_HALF_DOWN
Rounding mode to round to nearest neighbor, where an equidistant value is rounded down.- See Also:
MathContext.ROUND_HALF_DOWN, Constant Field Values- Status:
- Stable ICU 2.0.
-
ROUND_HALF_EVEN
public static final int ROUND_HALF_EVEN
Rounding mode to round to nearest neighbor, where an equidistant value is rounded to the nearest even neighbor.- See Also:
MathContext.ROUND_HALF_EVEN, Constant Field Values- Status:
- Stable ICU 2.0.
-
ROUND_HALF_UP
public static final int ROUND_HALF_UP
Rounding mode to round to nearest neighbor, where an equidistant value is rounded up.- See Also:
MathContext.ROUND_HALF_UP, Constant Field Values- Status:
- Stable ICU 2.0.
-
ROUND_UNNECESSARY
public static final int ROUND_UNNECESSARY
Rounding mode to assert that no rounding is necessary.- See Also:
MathContext.ROUND_UNNECESSARY, Constant Field Values- Status:
- Stable ICU 2.0.
-
ROUND_UP
public static final int ROUND_UP
Rounding mode to round away from zero.- See Also:
MathContext.ROUND_UP, Constant Field Values- Status:
- Stable ICU 2.0.
-
-
Constructor Detail
-
BigDecimal
public BigDecimal(BigDecimal bd)
Constructs aBigDecimalobject from ajava.math.BigDecimal.Constructs a
BigDecimalas though the parameter had been represented as aString(using itstoStringmethod) and theBigDecimal(java.lang.String)constructor had then been used. The parameter must not benull.(Note: this constructor is provided only in the
com.ibm.icu.mathversion of the BigDecimal class. It would not be present in ajava.mathversion.)- Parameters:
bd- TheBigDecimalto be translated.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(BigInteger bi)
Constructs aBigDecimalobject from aBigInteger, with scale 0.Constructs a
BigDecimalwhich is the exact decimal representation of theBigInteger, with a scale of zero. The value of theBigDecimalis identical to the value of theBigInteger. The parameter must not benull.The
BigDecimalwill contain only decimal digits, prefixed with a leading minus sign (hyphen) if theBigIntegeris negative. A leading zero will be present only if theBigIntegeris zero.- Parameters:
bi- TheBigIntegerto be converted.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(BigInteger bi, int scale)
Constructs aBigDecimalobject from aBigIntegerand a scale.Constructs a
BigDecimalwhich is the exact decimal representation of theBigInteger, scaled by the second parameter, which may not be negative. The value of theBigDecimalis theBigIntegerdivided by ten to the power of the scale. TheBigIntegerparameter must not benull.The
BigDecimalwill contain only decimal digits, (with an embedded decimal point followed byscaledecimal digits if the scale is positive), prefixed with a leading minus sign (hyphen) if theBigIntegeris negative. A leading zero will be present only if theBigIntegeris zero.- Parameters:
bi- TheBigIntegerto be converted.scale- Theintspecifying the scale.- Throws:
NumberFormatException- If the scale is negative.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(char[] inchars)
Constructs aBigDecimalobject from an array of characters.Constructs a
BigDecimalas though aStringhad been constructed from the character array and theBigDecimal(java.lang.String)constructor had then been used. The parameter must not benull.Using this constructor is faster than using the
BigDecimal(String)constructor if the string is already available in character array form.- Parameters:
inchars- Thechar[]array containing the number to be converted.- Throws:
NumberFormatException- If the parameter is not a valid number.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(char[] inchars, int offset, int length)Constructs aBigDecimalobject from an array of characters.Constructs a
BigDecimalas though aStringhad been constructed from the character array (or a subarray of that array) and theBigDecimal(java.lang.String)constructor had then been used. The first parameter must not benull, and the subarray must be wholly contained within it.Using this constructor is faster than using the
BigDecimal(String)constructor if the string is already available within a character array.- Parameters:
inchars- Thechar[]array containing the number to be converted.offset- Theintoffset into the array of the start of the number to be converted.length- Theintlength of the number.- Throws:
NumberFormatException- If the parameter is not a valid number for any reason.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(double num)
Constructs aBigDecimalobject directly from adouble.Constructs a
BigDecimalwhich is the exact decimal representation of the 64-bit signed binary floating point parameter.Note that this constructor it an exact conversion; it does not give the same result as converting
numto aStringusing theDouble.toString()method and then using theBigDecimal(java.lang.String)constructor. To get that result, use the staticvalueOf(double)method to construct aBigDecimalfrom adouble.- Parameters:
num- Thedoubleto be converted.- Throws:
NumberFormatException- If the parameter is infinite or not a number.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(int num)
Constructs aBigDecimalobject directly from aint.Constructs a
BigDecimalwhich is the exact decimal representation of the 32-bit signed binary integer parameter. TheBigDecimalwill contain only decimal digits, prefixed with a leading minus sign (hyphen) if the parameter is negative. A leading zero will be present only if the parameter is zero.- Parameters:
num- Theintto be converted.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(long num)
Constructs aBigDecimalobject directly from along.Constructs a
BigDecimalwhich is the exact decimal representation of the 64-bit signed binary integer parameter. TheBigDecimalwill contain only decimal digits, prefixed with a leading minus sign (hyphen) if the parameter is negative. A leading zero will be present only if the parameter is zero.- Parameters:
num- Thelongto be converted.- Status:
- Stable ICU 2.0.
-
BigDecimal
public BigDecimal(String string)
Constructs aBigDecimalobject from aString.Constructs a
BigDecimalfrom the parameter, which must not benulland must represent a valid number, as described formally in the documentation referred toabove.In summary, numbers in
Stringform must have at least one digit, may have a leading sign, may have a decimal point, and exponential notation may be used. They follow conventional syntax, and may not contain blanks.Some valid strings from which a
BigDecimalmight be constructed are:"0" -- Zero "12" -- A whole number "-76" -- A signed whole number "12.70" -- Some decimal places "+0.003" -- Plus sign is allowed "17." -- The same as 17 ".5" -- The same as 0.5 "4E+9" -- Exponential notation "0.73e-7" -- Exponential notation
(Exponential notation means that the number includes an optional sign and a power of ten following an '
E' that indicates how the decimal point will be shifted. Thus the"4E+9"above is just a short way of writing4000000000, and the"0.73e-7"is short for0.000000073.)The
BigDecimalconstructed from the String is in a standard form, with no blanks, as though theadd(BigDecimal)method had been used to add zero to the number with unlimited precision. If the string uses exponential notation (that is, includes aneor anE), then theBigDecimalnumber will be expressed in scientific notation (where the power of ten is adjusted so there is a single non-zero digit to the left of the decimal point); in this case if the number is zero then it will be expressed as the single digit 0, and if non-zero it will have an exponent unless that exponent would be 0. The exponent must fit in nine digits both before and after it is expressed in scientific notation.Any digits in the parameter must be decimal; that is,
Character.digit(c, 10)(wherecis the character in question) would not return -1.- Parameters:
string- TheStringto be converted.- Throws:
NumberFormatException- If the parameter is not a valid number.- Status:
- Stable ICU 2.0.
-
-
Method Detail
-
abs
public BigDecimal abs()
Returns a plainBigDecimalwhose value is the absolute value of thisBigDecimal.The same as
abs(MathContext), where the context isnew MathContext(0, MathContext.PLAIN).The length of the decimal part (the scale) of the result will be
this.scale()- Returns:
- A
BigDecimalwhose value is the absolute value of thisBigDecimal. - Status:
- Stable ICU 2.0.
-
abs
public BigDecimal abs(MathContext set)
Returns aBigDecimalwhose value is the absolute value of thisBigDecimal.If the current object is zero or positive, then the same result as invoking the
plus(MathContext)method with the same parameter is returned. Otherwise, the same result as invoking thenegate(MathContext)method with the same parameter is returned.- Parameters:
set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value is the absolute value of thisBigDecimal. - Status:
- Stable ICU 2.0.
-
add
public BigDecimal add(BigDecimal rhs)
Returns a plainBigDecimalwhose value isthis+rhs, using fixed point arithmetic.The same as
add(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).The length of the decimal part (the scale) of the result will be the maximum of the scales of the two operands.
- Parameters:
rhs- TheBigDecimalfor the right hand side of the addition.- Returns:
- A
BigDecimalwhose value isthis+rhs, using fixed point arithmetic. - Status:
- Stable ICU 2.0.
-
add
public BigDecimal add(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value isthis+rhs.Implements the addition (
+) operator (as defined in the decimal documentation, seeclass header), and returns the result as aBigDecimalobject.- Parameters:
rhs- TheBigDecimalfor the right hand side of the addition.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value isthis+rhs. - Status:
- Stable ICU 2.0.
-
compareTo
public int compareTo(BigDecimal rhs)
Compares thisBigDecimalto another, using unlimited precision.The same as
compareTo(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).- Specified by:
compareToin interfaceComparable<BigDecimal>- Parameters:
rhs- TheBigDecimalfor the right hand side of the comparison.- Returns:
- An
intwhose value is -1, 0, or 1 asthisis numerically less than, equal to, or greater thanrhs. - Status:
- Stable ICU 2.0.
-
compareTo
public int compareTo(BigDecimal rhs, MathContext set)
Compares thisBigDecimalto another.Implements numeric comparison, (as defined in the decimal documentation, see
class header), and returns a result of typeint.The result will be:
-1 if the current object is less than the first parameter 0 if the current object is equal to the first parameter 1 if the current object is greater than the first parameter. A
compareTo(BigDecimal)method is also provided.- Parameters:
rhs- TheBigDecimalfor the right hand side of the comparison.set- TheMathContextarithmetic settings.- Returns:
- An
intwhose value is -1, 0, or 1 asthisis numerically less than, equal to, or greater thanrhs. - Status:
- Stable ICU 2.0.
-
divide
public BigDecimal divide(BigDecimal rhs)
Returns a plainBigDecimalwhose value isthis/rhs, using fixed point arithmetic.The same as
divide(BigDecimal, int), where theBigDecimalisrhs, and the rounding mode isMathContext.ROUND_HALF_UP. The length of the decimal part (the scale) of the result will be the same as the scale of the current object, if the latter were formatted without exponential notation.- Parameters:
rhs- TheBigDecimalfor the right hand side of the division.- Returns:
- A plain
BigDecimalwhose value isthis/rhs, using fixed point arithmetic. - Throws:
ArithmeticException- Ifrhsis zero.- Status:
- Stable ICU 2.0.
-
divide
public BigDecimal divide(BigDecimal rhs, int round)
Returns a plainBigDecimalwhose value isthis/rhs, using fixed point arithmetic and a rounding mode.The same as
divide(BigDecimal, int, int), where theBigDecimalisrhs, and the second parameter isthis.scale(), and the third isround.The length of the decimal part (the scale) of the result will therefore be the same as the scale of the current object, if the latter were formatted without exponential notation.
- Parameters:
rhs- TheBigDecimalfor the right hand side of the division.round- Theintrounding mode to be used for the division (see theMathContextclass).- Returns:
- A plain
BigDecimalwhose value isthis/rhs, using fixed point arithmetic and the specified rounding mode. - Throws:
IllegalArgumentException- ifroundis not a valid rounding mode.ArithmeticException- ifrhsis zero.ArithmeticException- ifroundisMathContext.ROUND_UNNECESSARYandthis.scale()is insufficient to represent the result exactly.- Status:
- Stable ICU 2.0.
-
divide
public BigDecimal divide(BigDecimal rhs, int scale, int round)
Returns a plainBigDecimalwhose value isthis/rhs, using fixed point arithmetic and a given scale and rounding mode.The same as
divide(BigDecimal, MathContext), where theBigDecimalisrhs,new MathContext(0, MathContext.PLAIN, false, round), except that the length of the decimal part (the scale) to be used for the result is explicit rather than being taken fromthis.The length of the decimal part (the scale) of the result will be the same as the scale of the current object, if the latter were formatted without exponential notation.
- Parameters:
rhs- TheBigDecimalfor the right hand side of the division.scale- Theintscale to be used for the result.round- Theintrounding mode to be used for the division (see theMathContextclass).- Returns:
- A plain
BigDecimalwhose value isthis/rhs, using fixed point arithmetic and the specified rounding mode. - Throws:
IllegalArgumentException- ifroundis not a valid rounding mode.ArithmeticException- ifrhsis zero.ArithmeticException- ifscaleis negative.ArithmeticException- ifroundisMathContext.ROUND_UNNECESSARYandscaleis insufficient to represent the result exactly.- Status:
- Stable ICU 2.0.
-
divide
public BigDecimal divide(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value isthis/rhs.Implements the division (
/) operator (as defined in the decimal documentation, seeclass header), and returns the result as aBigDecimalobject.- Parameters:
rhs- TheBigDecimalfor the right hand side of the division.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value isthis/rhs. - Throws:
ArithmeticException- ifrhsis zero.- Status:
- Stable ICU 2.0.
-
divideInteger
public BigDecimal divideInteger(BigDecimal rhs)
Returns a plainBigDecimalwhose value is the integer part ofthis/rhs.The same as
divideInteger(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).- Parameters:
rhs- TheBigDecimalfor the right hand side of the integer division.- Returns:
- A
BigDecimalwhose value is the integer part ofthis/rhs. - Throws:
ArithmeticException- ifrhsis zero.- Status:
- Stable ICU 2.0.
-
divideInteger
public BigDecimal divideInteger(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value is the integer part ofthis/rhs.Implements the integer division operator (as defined in the decimal documentation, see
class header), and returns the result as aBigDecimalobject.- Parameters:
rhs- TheBigDecimalfor the right hand side of the integer division.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value is the integer part ofthis/rhs. - Throws:
ArithmeticException- ifrhsis zero.ArithmeticException- if the result will not fit in the number of digits specified for the context.- Status:
- Stable ICU 2.0.
-
max
public BigDecimal max(BigDecimal rhs)
Returns a plainBigDecimalwhose value is the maximum ofthisandrhs.The same as
max(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).- Parameters:
rhs- TheBigDecimalfor the right hand side of the comparison.- Returns:
- A
BigDecimalwhose value is the maximum ofthisandrhs. - Status:
- Stable ICU 2.0.
-
max
public BigDecimal max(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value is the maximum ofthisandrhs.Returns the larger of the current object and the first parameter.
If calling the
compareTo(BigDecimal, MathContext)method with the same parameters would return1or0, then the result of calling theplus(MathContext)method on the current object (using the sameMathContextparameter) is returned. Otherwise, the result of calling theplus(MathContext)method on the first parameter object (using the sameMathContextparameter) is returned.- Parameters:
rhs- TheBigDecimalfor the right hand side of the comparison.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value is the maximum ofthisandrhs. - Status:
- Stable ICU 2.0.
-
min
public BigDecimal min(BigDecimal rhs)
Returns a plainBigDecimalwhose value is the minimum ofthisandrhs.The same as
min(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).- Parameters:
rhs- TheBigDecimalfor the right hand side of the comparison.- Returns:
- A
BigDecimalwhose value is the minimum ofthisandrhs. - Status:
- Stable ICU 2.0.
-
min
public BigDecimal min(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value is the minimum ofthisandrhs.Returns the smaller of the current object and the first parameter.
If calling the
compareTo(BigDecimal, MathContext)method with the same parameters would return-1or0, then the result of calling theplus(MathContext)method on the current object (using the sameMathContextparameter) is returned. Otherwise, the result of calling theplus(MathContext)method on the first parameter object (using the sameMathContextparameter) is returned.- Parameters:
rhs- TheBigDecimalfor the right hand side of the comparison.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value is the minimum ofthisandrhs. - Status:
- Stable ICU 2.0.
-
multiply
public BigDecimal multiply(BigDecimal rhs)
Returns a plainBigDecimalwhose value isthis*rhs, using fixed point arithmetic.The same as
add(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).The length of the decimal part (the scale) of the result will be the sum of the scales of the operands, if they were formatted without exponential notation.
- Parameters:
rhs- TheBigDecimalfor the right hand side of the multiplication.- Returns:
- A
BigDecimalwhose value isthis*rhs, using fixed point arithmetic. - Status:
- Stable ICU 2.0.
-
multiply
public BigDecimal multiply(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value isthis*rhs.Implements the multiplication (
*) operator (as defined in the decimal documentation, seeclass header), and returns the result as aBigDecimalobject.- Parameters:
rhs- TheBigDecimalfor the right hand side of the multiplication.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value isthis*rhs. - Status:
- Stable ICU 2.0.
-
negate
public BigDecimal negate()
Returns a plainBigDecimalwhose value is-this.The same as
negate(MathContext), where the context isnew MathContext(0, MathContext.PLAIN).The length of the decimal part (the scale) of the result will be be
this.scale()- Returns:
- A
BigDecimalwhose value is-this. - Status:
- Stable ICU 2.0.
-
negate
public BigDecimal negate(MathContext set)
Returns aBigDecimalwhose value is-this.Implements the negation (Prefix
-) operator (as defined in the decimal documentation, seeclass header), and returns the result as aBigDecimalobject.- Parameters:
set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value is-this. - Status:
- Stable ICU 2.0.
-
plus
public BigDecimal plus()
Returns a plainBigDecimalwhose value is+this. Note thatthisis not necessarily a plainBigDecimal, but the result will always be.The same as
plus(MathContext), where the context isnew MathContext(0, MathContext.PLAIN).The length of the decimal part (the scale) of the result will be be
this.scale()- Returns:
- A
BigDecimalwhose value is+this. - Status:
- Stable ICU 2.0.
-
plus
public BigDecimal plus(MathContext set)
Returns aBigDecimalwhose value is+this.Implements the plus (Prefix
+) operator (as defined in the decimal documentation, seeclass header), and returns the result as aBigDecimalobject.This method is useful for rounding or otherwise applying a context to a decimal value.
- Parameters:
set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value is+this. - Status:
- Stable ICU 2.0.
-
pow
public BigDecimal pow(BigDecimal rhs)
Returns a plainBigDecimalwhose value isthis**rhs, using fixed point arithmetic.The same as
pow(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).The parameter is the power to which the
thiswill be raised; it must be in the range 0 through 999999999, and must have a decimal part of zero. Note that these restrictions may be removed in the future, so they should not be used as a test for a whole number.In addition, the power must not be negative, as no
MathContextis used and so the result would then always be 0.- Parameters:
rhs- TheBigDecimalfor the right hand side of the operation (the power).- Returns:
- A
BigDecimalwhose value isthis**rhs, using fixed point arithmetic. - Throws:
ArithmeticException- ifrhsis out of range or is not a whole number.- Status:
- Stable ICU 2.0.
-
pow
public BigDecimal pow(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value isthis**rhs.Implements the power (
^) operator (as defined in the decimal documentation, seeclass header), and returns the result as aBigDecimalobject.The first parameter is the power to which the
thiswill be raised; it must be in the range -999999999 through 999999999, and must have a decimal part of zero. Note that these restrictions may be removed in the future, so they should not be used as a test for a whole number.If the
digitssetting of theMathContextparameter is 0, the power must be zero or positive.- Parameters:
rhs- TheBigDecimalfor the right hand side of the operation (the power).set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value isthis**rhs. - Throws:
ArithmeticException- ifrhsis out of range or is not a whole number.- Status:
- Stable ICU 2.0.
-
remainder
public BigDecimal remainder(BigDecimal rhs)
Returns a plainBigDecimalwhose value is the remainder ofthis/rhs, using fixed point arithmetic.The same as
remainder(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).This is not the modulo operator -- the result may be negative.
- Parameters:
rhs- TheBigDecimalfor the right hand side of the remainder operation.- Returns:
- A
BigDecimalwhose value is the remainder ofthis/rhs, using fixed point arithmetic. - Throws:
ArithmeticException- ifrhsis zero.- Status:
- Stable ICU 2.0.
-
remainder
public BigDecimal remainder(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value is the remainder ofthis/rhs.Implements the remainder operator (as defined in the decimal documentation, see
class header), and returns the result as aBigDecimalobject.This is not the modulo operator -- the result may be negative.
- Parameters:
rhs- TheBigDecimalfor the right hand side of the remainder operation.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value is the remainder ofthis+rhs. - Throws:
ArithmeticException- ifrhsis zero.ArithmeticException- if the integer part of the result will not fit in the number of digits specified for the context.- Status:
- Stable ICU 2.0.
-
subtract
public BigDecimal subtract(BigDecimal rhs)
Returns a plainBigDecimalwhose value isthis-rhs, using fixed point arithmetic.The same as
subtract(BigDecimal, MathContext), where theBigDecimalisrhs, and the context isnew MathContext(0, MathContext.PLAIN).The length of the decimal part (the scale) of the result will be the maximum of the scales of the two operands.
- Parameters:
rhs- TheBigDecimalfor the right hand side of the subtraction.- Returns:
- A
BigDecimalwhose value isthis-rhs, using fixed point arithmetic. - Status:
- Stable ICU 2.0.
-
subtract
public BigDecimal subtract(BigDecimal rhs, MathContext set)
Returns aBigDecimalwhose value isthis-rhs.Implements the subtraction (
-) operator (as defined in the decimal documentation, seeclass header), and returns the result as aBigDecimalobject.- Parameters:
rhs- TheBigDecimalfor the right hand side of the subtraction.set- TheMathContextarithmetic settings.- Returns:
- A
BigDecimalwhose value isthis-rhs. - Status:
- Stable ICU 2.0.
-
byteValueExact
public byte byteValueExact()
Converts thisBigDecimalto abyte. If theBigDecimalhas a non-zero decimal part or is out of the possible range for abyte(8-bit signed integer) result then anArithmeticExceptionis thrown.- Returns:
- A
byteequal in value tothis. - Throws:
ArithmeticException- ifthishas a non-zero decimal part, or will not fit in abyte.- Status:
- Stable ICU 2.0.
-
doubleValue
public double doubleValue()
Converts thisBigDecimalto adouble. If theBigDecimalis out of the possible range for adouble(64-bit signed floating point) result then anArithmeticExceptionis thrown.The double produced is identical to result of expressing the
BigDecimalas aStringand then converting it using theDouble(String)constructor; this can result in values ofDouble.NEGATIVE_INFINITYorDouble.POSITIVE_INFINITY.- Specified by:
doubleValuein classNumber- Returns:
- A
doublecorresponding tothis. - Status:
- Stable ICU 2.0.
-
equals
public boolean equals(Object obj)
Compares thisBigDecimalwithrhsfor equality.If the parameter is
null, or is not an instance of the BigDecimal type, or is not exactly equal to the currentBigDecimalobject, then false is returned. Otherwise, true is returned."Exactly equal", here, means that the
Stringrepresentations of theBigDecimalnumbers are identical (they have the same characters in the same sequence).The
compareTo(BigDecimal, MathContext)method should be used for more general comparisons.- Overrides:
equalsin classObject- Parameters:
obj- TheObjectfor the right hand side of the comparison.- Returns:
- A
booleanwhose value true if and only if the operands have identical string representations. - Throws:
ClassCastException- ifrhscannot be cast to aBigDecimalobject.- See Also:
compareTo(BigDecimal),compareTo(BigDecimal, MathContext)- Status:
- Stable ICU 2.0.
-
floatValue
public float floatValue()
Converts thisBigDecimalto afloat. If theBigDecimalis out of the possible range for afloat(32-bit signed floating point) result then anArithmeticExceptionis thrown.The float produced is identical to result of expressing the
BigDecimalas aStringand then converting it using theFloat(String)constructor; this can result in values ofFloat.NEGATIVE_INFINITYorFloat.POSITIVE_INFINITY.- Specified by:
floatValuein classNumber- Returns:
- A
floatcorresponding tothis. - Status:
- Stable ICU 2.0.
-
format
public String format(int before, int after)
Returns theStringrepresentation of thisBigDecimal, modified by layout parameters.This method is provided as a primitive for use by more sophisticated classes, such as
DecimalFormat, that can apply locale-sensitive editing of the result. The level of formatting that it provides is a necessary part of the BigDecimal class as it is sensitive to and must follow the calculation and rounding rules for BigDecimal arithmetic. However, if the function is provided elsewhere, it may be removed from this class.The parameters, for both forms of the
formatmethod are all of typeint. A value of -1 for any parameter indicates that the default action or value for that parameter should be used.The parameters,
beforeandafter, specify the number of characters to be used for the integer part and decimal part of the result respectively. Exponential notation is not used. If either parameter is -1 (which indicates the default action), the number of characters used will be exactly as many as are needed for that part.beforemust be a positive number; if it is larger than is needed to contain the integer part, that part is padded on the left with blanks to the requested length. Ifbeforeis not large enough to contain the integer part of the number (including the sign, for negative numbers) an exception is thrown.aftermust be a non-negative number; if it is not the same size as the decimal part of the number, the number will be rounded (or extended with zeros) to fit. Specifying 0 forafterwill cause the number to be rounded to an integer (that is, it will have no decimal part or decimal point). The rounding method will be the default,MathContext.ROUND_HALF_UP.Other rounding methods, and the use of exponential notation, can be selected by using
format(int,int,int,int,int,int). Using the two-parameter form of the method has exactly the same effect as using the six-parameter form with the final four parameters all being -1.- Parameters:
before- Theintspecifying the number of places before the decimal point. Use -1 for 'as many as are needed'.after- Theintspecifying the number of places after the decimal point. Use -1 for 'as many as are needed'.- Returns:
- A
Stringrepresenting thisBigDecimal, laid out according to the specified parameters - Throws:
ArithmeticException- if the number cannot be laid out as requested.IllegalArgumentException- if a parameter is out of range.- See Also:
toString(),toCharArray()- Status:
- Stable ICU 2.0.
-
format
public String format(int before, int after, int explaces, int exdigits, int exformint, int exround)
Returns theStringrepresentation of thisBigDecimal, modified by layout parameters and allowing exponential notation.This method is provided as a primitive for use by more sophisticated classes, such as
DecimalFormat, that can apply locale-sensitive editing of the result. The level of formatting that it provides is a necessary part of the BigDecimal class as it is sensitive to and must follow the calculation and rounding rules for BigDecimal arithmetic. However, if the function is provided elsewhere, it may be removed from this class.The parameters are all of type
int. A value of -1 for any parameter indicates that the default action or value for that parameter should be used.The first two parameters (
beforeandafter) specify the number of characters to be used for the integer part and decimal part of the result respectively, as defined forformat(int,int). If either of these is -1 (which indicates the default action), the number of characters used will be exactly as many as are needed for that part.The remaining parameters control the use of exponential notation and rounding. Three (
explaces,exdigits, andexform) control the exponent part of the result. As before, the default action for any of these parameters may be selected by using the value -1.explacesmust be a positive number; it sets the number of places (digits after the sign of the exponent) to be used for any exponent part, the default (whenexplacesis -1) being to use as many as are needed. Ifexplacesis not -1, space is always reserved for an exponent; if one is not needed (for example, if the exponent will be 0) thenexplaces+2 blanks are appended to the result. (This preserves vertical alignment of similarly formatted numbers in a monospace font.) Ifexplacesis not -1 and is not large enough to contain the exponent, an exception is thrown.exdigitssets the trigger point for use of exponential notation. If, before any rounding, the number of places needed before the decimal point exceedsexdigits, or if the absolute value of the result is less than0.000001, then exponential form will be used, provided thatexdigitswas specified. Whenexdigitsis -1, exponential notation will never be used. If 0 is specified forexdigits, exponential notation is always used unless the exponent would be 0.exformsets the form for exponential notation (if needed). It may be eitherMathContext.SCIENTIFICorMathContext.ENGINEERING. If the latter, engineering, form is requested, up to three digits (plus sign, if negative) may be needed for the integer part of the result (before). Otherwise, only one digit (plus sign, if negative) is needed.Finally, the sixth argument,
exround, selects the rounding algorithm to be used, and must be one of the values indicated by a public constant in theMathContextclass whose name starts withROUND_. The default (ROUND_HALF_UP) may also be selected by using the value -1, as before.The special value
MathContext.ROUND_UNNECESSARYmay be used to detect whether non-zero digits are discarded -- ifexroundhas this value than if non-zero digits would be discarded (rounded) during formatting then anArithmeticExceptionis thrown.- Parameters:
before- Theintspecifying the number of places before the decimal point. Use -1 for 'as many as are needed'.after- Theintspecifying the number of places after the decimal point. Use -1 for 'as many as are needed'.explaces- Theintspecifying the number of places to be used for any exponent. Use -1 for 'as many as are needed'.exdigits- Theintspecifying the trigger (digits before the decimal point) which if exceeded causes exponential notation to be used. Use 0 to force exponential notation. Use -1 to force plain notation (no exponential notation).exformint- Theintspecifying the form of exponential notation to be used (MathContext.SCIENTIFICorMathContext.ENGINEERING).exround- Theintspecifying the rounding mode to use. Use -1 for the default,MathContext.ROUND_HALF_UP.- Returns:
- A
Stringrepresenting thisBigDecimal, laid out according to the specified parameters - Throws:
ArithmeticException- if the number cannot be laid out as requested.IllegalArgumentException- if a parameter is out of range.- See Also:
toString(),toCharArray()- Status:
- Stable ICU 2.0.
-
hashCode
public int hashCode()
Returns the hashcode for thisBigDecimal. This hashcode is suitable for use by thejava.util.Hashtableclass.Note that two
BigDecimalobjects are only guaranteed to produce the same hashcode if they are exactly equal (that is, theStringrepresentations of theBigDecimalnumbers are identical -- they have the same characters in the same sequence).
-
intValue
public int intValue()
Converts thisBigDecimalto anint. If theBigDecimalhas a non-zero decimal part it is discarded. If theBigDecimalis out of the possible range for anint(32-bit signed integer) result then only the low-order 32 bits are used. (That is, the number may be decapitated.) To avoid unexpected errors when these conditions occur, use theintValueExact()method.
-
intValueExact
public int intValueExact()
Converts thisBigDecimalto anint. If theBigDecimalhas a non-zero decimal part or is out of the possible range for anint(32-bit signed integer) result then anArithmeticExceptionis thrown.- Returns:
- An
intequal in value tothis. - Throws:
ArithmeticException- ifthishas a non-zero decimal part, or will not fit in anint.- Status:
- Stable ICU 2.0.
-
longValue
public long longValue()
Converts thisBigDecimalto along. If theBigDecimalhas a non-zero decimal part it is discarded. If theBigDecimalis out of the possible range for along(64-bit signed integer) result then only the low-order 64 bits are used. (That is, the number may be decapitated.) To avoid unexpected errors when these conditions occur, use thelongValueExact()method.
-
longValueExact
public long longValueExact()
Converts thisBigDecimalto along. If theBigDecimalhas a non-zero decimal part or is out of the possible range for along(64-bit signed integer) result then anArithmeticExceptionis thrown.- Returns:
- A
longequal in value tothis. - Throws:
ArithmeticException- ifthishas a non-zero decimal part, or will not fit in along.- Status:
- Stable ICU 2.0.
-
movePointLeft
public BigDecimal movePointLeft(int n)
Returns a plainBigDecimalwhose decimal point has been moved to the left by a specified number of positions. The parameter,n, specifies the number of positions to move the decimal point. That is, ifnis 0 or positive, the number returned is given by:this.multiply(TEN.pow(new BigDecimal(-n)))nmay be negative, in which case the method returns the same result asmovePointRight(-n).- Parameters:
n- Theintspecifying the number of places to move the decimal point leftwards.- Returns:
- A
BigDecimalderived fromthis, with the decimal point movednplaces to the left. - Status:
- Stable ICU 2.0.
-
movePointRight
public BigDecimal movePointRight(int n)
Returns a plainBigDecimalwhose decimal point has been moved to the right by a specified number of positions. The parameter,n, specifies the number of positions to move the decimal point. That is, ifnis 0 or positive, the number returned is given by:this.multiply(TEN.pow(new BigDecimal(n)))nmay be negative, in which case the method returns the same result asmovePointLeft(-n).- Parameters:
n- Theintspecifying the number of places to move the decimal point rightwards.- Returns:
- A
BigDecimalderived fromthis, with the decimal point movednplaces to the right. - Status:
- Stable ICU 2.0.
-
scale
public int scale()
Returns the scale of thisBigDecimal. Returns a non-negativeintwhich is the scale of the number. The scale is the number of digits in the decimal part of the number if the number were formatted without exponential notation.- Returns:
- An
intwhose value is the scale of thisBigDecimal. - Status:
- Stable ICU 2.0.
-
setScale
public BigDecimal setScale(int scale)
Returns a plainBigDecimalwith a given scale.If the given scale (which must be zero or positive) is the same as or greater than the length of the decimal part (the scale) of this
BigDecimalthen trailing zeros will be added to the decimal part as necessary.If the given scale is less than the length of the decimal part (the scale) of this
BigDecimalthen trailing digits will be removed, and in this case anArithmeticExceptionis thrown if any discarded digits are non-zero.The same as
setScale(int, int), where the first parameter is the scale, and the second isMathContext.ROUND_UNNECESSARY.- Parameters:
scale- Theintspecifying the scale of the resultingBigDecimal.- Returns:
- A plain
BigDecimalwith the given scale. - Throws:
ArithmeticException- ifscaleis negative.ArithmeticException- if reducing scale would discard non-zero digits.- Status:
- Stable ICU 2.0.
-
setScale
public BigDecimal setScale(int scale, int round)
Returns a plainBigDecimalwith a given scale.If the given scale (which must be zero or positive) is the same as or greater than the length of the decimal part (the scale) of this
BigDecimalthen trailing zeros will be added to the decimal part as necessary.If the given scale is less than the length of the decimal part (the scale) of this
BigDecimalthen trailing digits will be removed, and the rounding mode given by the second parameter is used to determine if the remaining digits are affected by a carry. In this case, anIllegalArgumentExceptionis thrown ifroundis not a valid rounding mode.If
roundisMathContext.ROUND_UNNECESSARY, anArithmeticExceptionis thrown if any discarded digits are non-zero.- Parameters:
scale- Theintspecifying the scale of the resultingBigDecimal.round- Theintrounding mode to be used for the division (see theMathContextclass).- Returns:
- A plain
BigDecimalwith the given scale. - Throws:
IllegalArgumentException- ifroundis not a valid rounding mode.ArithmeticException- ifscaleis negative.ArithmeticException- ifroundisMathContext.ROUND_UNNECESSARY, and reducing scale would discard non-zero digits.- Status:
- Stable ICU 2.0.
-
shortValueExact
public short shortValueExact()
Converts thisBigDecimalto ashort. If theBigDecimalhas a non-zero decimal part or is out of the possible range for ashort(16-bit signed integer) result then anArithmeticExceptionis thrown.- Returns:
- A
shortequal in value tothis. - Throws:
ArithmeticException- ifthishas a non-zero decimal part, or will not fit in ashort.- Status:
- Stable ICU 2.0.
-
signum
public int signum()
Returns the sign of thisBigDecimal, as anint. This returns the signum function value that represents the sign of thisBigDecimal. That is, -1 if theBigDecimalis negative, 0 if it is numerically equal to zero, or 1 if it is positive.- Returns:
- An
intwhich is -1 if theBigDecimalis negative, 0 if it is numerically equal to zero, or 1 if it is positive. - Status:
- Stable ICU 2.0.
-
toBigDecimal
public BigDecimal toBigDecimal()
Converts thisBigDecimalto ajava.math.BigDecimal.This is an exact conversion; the result is the same as if the
BigDecimalwere formatted as a plain number without any rounding or exponent and then thejava.math.BigDecimal(java.lang.String)constructor were used to construct the result.(Note: this method is provided only in the
com.ibm.icu.mathversion of the BigDecimal class. It would not be present in ajava.mathversion.)- Returns:
- The
java.math.BigDecimalequal in value to thisBigDecimal. - Status:
- Stable ICU 2.0.
-
toBigInteger
public BigInteger toBigInteger()
Converts thisBigDecimalto ajava.math.BigInteger.Any decimal part is truncated (discarded). If an exception is desired should the decimal part be non-zero, use
toBigIntegerExact().- Returns:
- The
java.math.BigIntegerequal in value to the integer part of thisBigDecimal. - Status:
- Stable ICU 2.0.
-
toBigIntegerExact
public BigInteger toBigIntegerExact()
Converts thisBigDecimalto ajava.math.BigInteger.An exception is thrown if the decimal part (if any) is non-zero.
- Returns:
- The
java.math.BigIntegerequal in value to the integer part of thisBigDecimal. - Throws:
ArithmeticException- ifthishas a non-zero decimal part.- Status:
- Stable ICU 2.0.
-
toCharArray
public char[] toCharArray()
Returns theBigDecimalas a character array. The result of this method is the same as using the sequencetoString().toCharArray(), but avoids creating the intermediateStringandchar[]objects.- Returns:
- The
char[]array corresponding to thisBigDecimal. - Status:
- Stable ICU 2.0.
-
toString
public String toString()
Returns theBigDecimalas aString. This returns aStringthat exactly represents thisBigDecimal, as defined in the decimal documentation (seeclass header).By definition, using the
BigDecimal(String)constructor on the resultStringwill create aBigDecimalthat is exactly equal to the originalBigDecimal.- Overrides:
toStringin classObject- Returns:
- The
Stringexactly corresponding to thisBigDecimal. - See Also:
format(int, int),format(int, int, int, int, int, int),toCharArray()- Status:
- Stable ICU 2.0.
-
unscaledValue
public BigInteger unscaledValue()
Returns the number as aBigIntegerafter removing the scale. That is, the number is expressed as a plain number, any decimal point is then removed (retaining the digits of any decimal part), and the result is then converted to aBigInteger.- Returns:
- The
java.math.BigIntegerequal in value to thisBigDecimalmultiplied by ten to the power ofthis.scale(). - Status:
- Stable ICU 2.0.
-
valueOf
public static BigDecimal valueOf(double dub)
Translates adoubleto aBigDecimal.Returns a
BigDecimalwhich is the decimal representation of the 64-bit signed binary floating point parameter. If the parameter is infinite, or is not a number (NaN), aNumberFormatExceptionis thrown.The number is constructed as though
numhad been converted to aStringusing theDouble.toString()method and theBigDecimal(java.lang.String)constructor had then been used. This is typically not an exact conversion.- Parameters:
dub- Thedoubleto be translated.- Returns:
- The
BigDecimalequal in value todub. - Throws:
NumberFormatException- if the parameter is infinite or not a number.- Status:
- Stable ICU 2.0.
-
valueOf
public static BigDecimal valueOf(long lint)
Translates alongto aBigDecimal. That is, returns a plainBigDecimalwhose value is equal to the givenlong.- Parameters:
lint- Thelongto be translated.- Returns:
- The
BigDecimalequal in value tolint. - Status:
- Stable ICU 2.0.
-
valueOf
public static BigDecimal valueOf(long lint, int scale)
Translates alongto aBigDecimalwith a given scale. That is, returns a plainBigDecimalwhose unscaled value is equal to the givenlong, adjusted by the second parameter,scale.The result is given by:
(new BigDecimal(lint)).divide(TEN.pow(new BigDecimal(scale)))A
NumberFormatExceptionis thrown ifscaleis negative.- Parameters:
lint- Thelongto be translated.scale- Theintscale to be applied.- Returns:
- The
BigDecimalequal in value tolint. - Throws:
NumberFormatException- if the scale is negative.- Status:
- Stable ICU 2.0.
-
-