BigDecimal

Immutable, arbitrary-precision signed decimal numbers. A {@code BigDecimal} consists of an arbitrary precision integer <i>unscaled value</i> and a 32-bit integer <i>scale</i>. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the {@code BigDecimal} is therefore <code>(unscaledValue &times; 10!(sup)-scale</sup>)</code>.

<p>The {@code BigDecimal} class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The {@link #toString} method provides a canonical representation of a {@code BigDecimal}.

<p>The {@code BigDecimal} class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an exception is thrown; otherwise, calculations can be carried out to a chosen precision and rounding mode by supplying an appropriate {@link MathContext} object to the operation. In either case, eight <em>rounding modes</em> are provided for the control of rounding. Using the integer fields in this class (such as {@link #ROUND_HALF_UP}) to represent rounding mode is deprecated; the enumeration values of the {@code RoundingMode} {@code enum}, (such as {@link RoundingMode#HALF_UP}) should be used instead.

<p>When a {@code MathContext} object is supplied with a precision setting of 0 (for example, {@link MathContext#UNLIMITED}), arithmetic operations are exact, as are the arithmetic methods which take no {@code MathContext} object. (This is the only behavior that was supported in releases prior to 5.) As a corollary of computing the exact result, the rounding mode setting of a {@code MathContext} object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an {@code ArithmeticException} is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

<p>When the precision setting is not 0, the rules of {@code BigDecimal} arithmetic are broadly compatible with selected modes of operation of the arithmetic defined in ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 (section 7.4). Unlike those standards, {@code BigDecimal} includes many rounding modes, which were mandatory for division in {@code BigDecimal} releases prior to 5. Any conflicts between these ANSI standards and the {@code BigDecimal} specification are resolved in favor of {@code BigDecimal}.

<p>Since the same numerical value can have different representations (with different scales), the rules of arithmetic and rounding must specify both the numerical result and the scale used in the result's representation.

More...

Constructors

this
this(BigInteger intVal, long val, int scale, int prec)

Trusted package private constructor. Trusted simply means if val is INFLATED, intVal could not be null and if intVal is null, val could not be INFLATED.

this
this(char[] inp)
Undocumented in source.
this
this(string val)

Translates the string representation of a {@code BigDecimal} into a {@code BigDecimal}. The string representation consists of an optional sign, {@code '+'} (<code> '&#92;u002B'</code>) or {@code '-'} (<code>'&#92;u002D'</code>), followed by a sequence of zero or more decimal digits ("the integer"), optionally followed by a fraction, optionally followed by an exponent.

this
this(double val)
Undocumented in source.
this
this(BigInteger val)
Undocumented in source.
this
this(int val)

Translates an {@code int} into a {@code BigDecimal}. The scale of the {@code BigDecimal} is zero.

this
this(long val)

Translates a {@code long} into a {@code BigDecimal}. The scale of the {@code BigDecimal} is zero.

Members

Functions

add
BigDecimal add(BigDecimal augend)
Undocumented in source. Be warned that the author may not have intended to support it.
byteValue
byte byteValue()

Returns the value of the specified number as a {@code byte}.

divide
BigDecimal divide(BigDecimal divisor, int roundingMode)
Undocumented in source. Be warned that the author may not have intended to support it.
divide
BigDecimal divide(BigDecimal divisor)
Undocumented in source. Be warned that the author may not have intended to support it.
divideToIntegralValue
BigDecimal divideToIntegralValue(BigDecimal divisor)
Undocumented in source. Be warned that the author may not have intended to support it.
doubleValue
double doubleValue()

Returns the value of the specified number as a {@code double}.

floatValue
float floatValue()

Returns the value of the specified number as a {@code float}.

intValue
int intValue()

Returns the value of the specified number as an {@code int}.

longValue
long longValue()

Returns the value of the specified number as a {@code long}.

movePointLeft
BigDecimal movePointLeft(int n)
Undocumented in source. Be warned that the author may not have intended to support it.
movePointRight
BigDecimal movePointRight(int n)
Undocumented in source. Be warned that the author may not have intended to support it.
multiply
BigDecimal multiply(BigDecimal multiplicand)

Returns a {@code BigDecimal} whose value is <code>(this &times; multiplicand)</code>, and whose scale is {@code (this.scale() + multiplicand.scale())}.

scale
int scale()

Returns the <i>scale</i> of this {@code BigDecimal}. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of {@code -3} means the unscaled value is multiplied by 1000.

setScale
BigDecimal setScale(int newScale, int t)
Undocumented in source. Be warned that the author may not have intended to support it.
setScale
BigDecimal setScale(int newScale)
Undocumented in source. Be warned that the author may not have intended to support it.
shortValue
short shortValue()

Returns the value of the specified number as a {@code short}.

signum
int signum()
Undocumented in source. Be warned that the author may not have intended to support it.
subtract
BigDecimal subtract(BigDecimal subtrahend)

Returns a {@code BigDecimal} whose value is {@code (this - subtrahend)}, and whose scale is {@code max(this.scale(), subtrahend.scale())}.

toBigInteger
BigInteger toBigInteger()

Converts this {@code BigDecimal} to a {@code BigInteger}. This conversion is analogous to the <i>narrowing primitive conversion</i> from {@code double} to {@code long} as defined in <cite>The Java&trade; Language Specification</cite>: any fractional part of this {@code BigDecimal} will be discarded. Note that this conversion can lose information about the precision of the {@code BigDecimal} value. <p> To have an exception thrown if the conversion is inexact (in other words if a nonzero fractional part is discarded), use the {@link #toBigIntegerExact()} method.

toBigIntegerExact
BigInteger toBigIntegerExact()

Converts this {@code BigDecimal} to a {@code BigInteger}, checking for lost information. An exception is thrown if this {@code BigDecimal} has a nonzero fractional part.

toPlainString
string toPlainString()

Returns a string representation of this {@code BigDecimal} without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result.

toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.

Static functions

valueOf
BigDecimal valueOf(long unscaledVal, int scale)
Undocumented in source. Be warned that the author may not have intended to support it.
valueOf
BigDecimal valueOf(long val)

Translates a {@code long} value into a {@code BigDecimal} with a scale of zero.

Static variables

INFLATED
long INFLATED;
Undocumented in source.
ONE
BigDecimal ONE;
Undocumented in source.
ZERO
BigDecimal ZERO;
Undocumented in source.
ZERO_THROUGH_TEN
BigDecimal[] ZERO_THROUGH_TEN;
Undocumented in source.

Variables

ROUND_CEILING
enum int ROUND_CEILING;
Undocumented in source.
ROUND_DOWN
enum int ROUND_DOWN;
Undocumented in source.
ROUND_FLOOR
enum int ROUND_FLOOR;
Undocumented in source.
ROUND_HALF_DOWN
enum int ROUND_HALF_DOWN;
Undocumented in source.
ROUND_HALF_EVEN
enum int ROUND_HALF_EVEN;
Undocumented in source.
ROUND_HALF_UP
enum int ROUND_HALF_UP;
Undocumented in source.
ROUND_UNNECESSARY
enum int ROUND_UNNECESSARY;
Undocumented in source.
ROUND_UP
enum int ROUND_UP;
Undocumented in source.

Inherited Members

From Number

intValue
int intValue()

Returns the value of the specified number as an {@code int}.

longValue
long longValue()

Returns the value of the specified number as a {@code long}.

floatValue
float floatValue()

Returns the value of the specified number as a {@code float}.

doubleValue
double doubleValue()

Returns the value of the specified number as a {@code double}.

byteValue
byte byteValue()

Returns the value of the specified number as a {@code byte}.

shortValue
short shortValue()

Returns the value of the specified number as a {@code short}.

toString
string toString()
Undocumented in source.

Detailed Description

<p>In general the rounding modes and precision setting determine how operations return results with a limited number of digits when the exact result has more digits (perhaps infinitely many in the case of division and square root) than the number of digits returned.

First, the total number of digits to return is specified by the {@code MathContext}'s {@code precision} setting; this determines the result's <i>precision</i>. The digit count starts from the leftmost nonzero digit of the exact result. The rounding mode determines how any discarded trailing digits affect the returned result.

<p>For all arithmetic operators , the operation is carried out as though an exact intermediate result were first calculated and then rounded to the number of digits specified by the precision setting (if necessary), using the selected rounding mode. If the exact result is not returned, some digit positions of the exact result are discarded. When rounding increases the magnitude of the returned result, it is possible for a new digit position to be created by a carry propagating to a leading {@literal "9"} digit. For example, rounding the value 999.9 to three digits rounding up would be numerically equal to one thousand, represented as 100&times;10!(sup)1</sup>. In such cases, the new {@literal "1"} is the leading digit position of the returned result.

<p>Besides a logical exact result, each arithmetic operation has a preferred scale for representing a result. The preferred scale for each operation is listed in the table below.

<table class="striped" style="text-align:left"> <caption>Preferred Scales for Results of Arithmetic Operations </caption> <thead> <tr><th scope="col">Operation</th><th scope="col">Preferred Scale of Result</th></tr> </thead> <tbody> <tr><th scope="row">Add</th><td>max(addend.scale(), augend.scale())</td> <tr><th scope="row">Subtract</th><td>max(minuend.scale(), subtrahend.scale())</td> <tr><th scope="row">Multiply</th><td>multiplier.scale() + multiplicand.scale()</td> <tr><th scope="row">Divide</th><td>dividend.scale() - divisor.scale()</td> <tr><th scope="row">Square root</th><td>radicand.scale()/2</td> </tbody> </table>

These scales are the ones used by the methods which return exact arithmetic results; except that an exact divide may have to use a larger scale since the exact result may have more digits. For example, {@code 1/32} is {@code 0.03125}.

<p>Before rounding, the scale of the logical exact intermediate result is the preferred scale for that operation. If the exact numerical result cannot be represented in {@code precision} digits, rounding selects the set of digits to return and the scale of the result is reduced from the scale of the intermediate result to the least scale which can represent the {@code precision} digits actually returned. If the exact result can be represented with at most {@code precision} digits, the representation of the result with the scale closest to the preferred scale is returned. In particular, an exactly representable quotient may be represented in fewer than {@code precision} digits by removing trailing zeros and decreasing the scale. For example, rounding to three digits using the {@linkplain RoundingMode#FLOOR floor} rounding mode, <br>

{@code 19/100 = 0.19 // integer=19, scale=2} <br>

but!(br)

{@code 21/110 = 0.190 // integer=190, scale=3} <br>

<p>Note that for add, subtract, and multiply, the reduction in scale will equal the number of digit positions of the exact result which are discarded. If the rounding causes a carry propagation to create a new high-order digit position, an additional digit of the result is discarded than when no new digit position is created.

<p>Other methods may have slightly different rounding semantics. For example, the result of the {@code pow} method using the {@linkplain #pow(int, MathContext) specified algorithm} can occasionally differ from the rounded mathematical result by more than one unit in the last place, one <i>{@linkplain #ulp() ulp}</i>.

<p>Two types of operations are provided for manipulating the scale of a {@code BigDecimal}: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations ({@link #setScale setScale} and {@link #round round}) return a {@code BigDecimal} whose value is approximately (or exactly) equal to that of the operand, but whose scale or precision is the specified value; that is, they increase or decrease the precision of the stored number with minimal effect on its value. Decimal point motion operations ({@link #movePointLeft movePointLeft} and {@link #movePointRight movePointRight}) return a {@code BigDecimal} created from the operand by moving the decimal point a specified distance in the specified direction.

<p>For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of {@code BigDecimal} methods. The pseudo-code expression {@code (i + j)} is shorthand for "a {@code BigDecimal} whose value is that of the {@code BigDecimal} {@code i} added to that of the {@code BigDecimal} {@code j}." The pseudo-code expression {@code (i == j)} is shorthand for "{@code true} if and only if the {@code BigDecimal} {@code i} represents the same value as the {@code BigDecimal} {@code j}." Other pseudo-code expressions are interpreted similarly. Square brackets are used to represent the particular {@code BigInteger} and scale pair defining a {@code BigDecimal} value; for example [19, 2] is the {@code BigDecimal} numerically equal to 0.19 having a scale of 2.

<p>All methods and constructors for this class throw {@code NullPointerException} when passed a {@code null} object reference for any input parameter.

@apiNote Care should be exercised if {@code BigDecimal} objects are used as keys in a {@link java.util.SortedMap SortedMap} or elements in a {@link java.util.SortedSet SortedSet} since {@code BigDecimal}'s <i>natural ordering</i> is <em>inconsistent with equals</em>. See {@link Comparable}, {@link java.util.SortedMap} or {@link java.util.SortedSet} for more information.

@see BigInteger @see MathContext @see RoundingMode @see java.util.SortedMap @see java.util.SortedSet @author Josh Bloch @author Mike Cowlishaw @author Joseph D. Darcy @author Sergey V. Kuksenko

Meta