APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.math

public Class BigDecimal

extends Number
implements Comparable<BigDecimal>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable
Static Imports
java.math.BigInteger.LONG_MASK

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If the scale is zero or positive, the scale is the number of digits to the right of the decimal point. If the scale is 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 BigDecimal is therefore (unscaledValue × 10-scale).

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString method provides a canonical representation of a BigDecimal.

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

When a MathContext object is supplied with a precision setting of 0 (for example, MathContext#UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. As a corollary of computing the exact result, the rounding mode setting of a 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 ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

When the precision setting is not 0, the rules of 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, BigDecimal includes many rounding modes. Any conflicts between these ANSI standards and the BigDecimal specification are resolved in favor of BigDecimal.

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. The different representations of the same numerical value are called members of the same cohort. The natural order of BigDecimal considers members of the same cohort to be equal to each other. In contrast, the equals method requires both the numerical value and representation to be the same for equality to hold. The results of methods like scale() and unscaledValue() will differ for numerically equal values with different representations.

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 MathContext's precision setting; this determines the result's precision. 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.

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 "9" digit. For example, rounding the value 999.9 to three digits rounding up would be numerically equal to one thousand, represented as 100×101. In such cases, the new "1" is the leading digit position of the returned result.

For methods and constructors with a MathContext parameter, if the result is inexact but the rounding mode is UNNECESSARY, an ArithmeticException will be thrown.

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.

Preferred Scales for Results of Arithmetic Operations
OperationPreferred Scale of Result
Addmax(addend.scale(), augend.scale())
Subtractmax(minuend.scale(), subtrahend.scale())
Multiplymultiplier.scale() + multiplicand.scale()
Dividedividend.scale() - divisor.scale()
Square rootceil(radicand.scale()/2.0)
nth rootceil((double) radicand.scale()/n)
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, 1/32 is 0.03125.

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 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 precision digits actually returned. If the exact result can be represented with at most 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 precision digits by removing trailing zeros and decreasing the scale. For example, rounding to three digits using the floor rounding mode,
19/100 = 0.19 // integer=19, scale=2
but
21/110 = 0.190 // integer=190, scale=3

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.

Other methods may have slightly different rounding semantics. For example, the result of the pow method using the specified algorithm can occasionally differ from the rounded mathematical result by more than one unit in the last place, one ulp.

Two types of operations are provided for manipulating the scale of a BigDecimal: scaling/rounding operations and decimal point motion operations. Scaling/rounding operations (setScale and round) return a 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 (movePointLeft and movePointRight) return a BigDecimal created from the operand by moving the decimal point a specified distance in the specified direction.

As a 32-bit integer, the set of values for the scale is large, but bounded. If the scale of a result would exceed the range of a 32-bit integer, either by overflow or underflow, the operation may throw an ArithmeticException.

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

All methods and constructors for this class throw NullPointerException when passed a null object reference for any input parameter.

Relation to IEEE 754 Decimal Arithmetic

Starting with its 2008 revision, the IEEE 754 Standard for Floating-point Arithmetic has covered decimal formats and operations. While there are broad similarities in the decimal arithmetic defined by IEEE 754 and by this class, there are notable differences as well. The fundamental similarity shared by BigDecimal and IEEE 754 decimal arithmetic is the conceptual operation of computing the mathematical infinitely precise real number value of an operation and then mapping that real number to a representable decimal floating-point value under a rounding policy. The rounding policy is called a rounding mode for BigDecimal and called a rounding-direction attribute in IEEE 754-2019. When the exact value is not representable, the rounding policy determines which of the two representable decimal values bracketing the exact value is selected as the computed result. The notion of a preferred scale/preferred exponent is also shared by both systems.

For differences, IEEE 754 includes several kinds of values not modeled by BigDecimal including negative zero, signed infinities, and NaN (not-a-number). IEEE 754 defines formats, which are parameterized by base (binary or decimal), number of digits of precision, and exponent range. A format determines the set of representable values. Most operations accept as input one or more values of a given format and produce a result in the same format. A BigDecimal's scale is equivalent to negating an IEEE 754 value's exponent. BigDecimal values do not have a format in the same sense; all values have the same possible range of scale/exponent and the unscaled value has arbitrary precision. Instead, for the BigDecimal operations taking a MathContext parameter, if the MathContext has a nonzero precision, the set of possible representable values for the result is determined by the precision of the MathContext argument. For example in BigDecimal, if a nonzero three-digit number and a nonzero four-digit number are multiplied together in the context of a MathContext object having a precision of three, the result will have three digits (assuming no overflow or underflow, etc.).

The rounding policies implemented by BigDecimal operations indicated by rounding modes are a proper superset of the IEEE 754 rounding-direction attributes.

BigDecimal arithmetic will most resemble IEEE 754 decimal arithmetic if a MathContext corresponding to an IEEE 754 decimal format, such as decimal64 or decimal128 is used to round all starting values and intermediate operations. The numerical values computed can differ if the exponent range of the IEEE 754 format being approximated is exceeded since a MathContext does not constrain the scale of BigDecimal results. Operations that would generate a NaN or exact infinity, such as dividing by zero, throw an ArithmeticException in BigDecimal arithmetic.

Algorithmic Complexity

Operations on BigDecimal values have a range of algorithmic complexities; in general, those complexities are a function of both the size of the unscaled value as well as the size of the scale. For example, an exact multiply of two BigDecimal values is subject to the same complexity constraints as BigInteger multiply of the unscaled values. In contrast, a BigDecimal value with a compact representation like new BigDecimal(1E-1000000000) has a toPlainString result with over one billion characters.

Operations may also allocate and compute on intermediate results, potentially those allocations may be as large as in proportion to the running time of the algorithm.

Users of BigDecimal concerned with bounding the running time or space of operations can screen out BigDecimal values with unscaled values or scales above a chosen magnitude.

API Note

Care should be exercised if BigDecimal objects are used as keys in a SortedMap or elements in a SortedSet since BigDecimal's natural ordering is inconsistent with equals. See Comparable, java.util.SortedMap or java.util.SortedSet for more information.

Since
1.1
External Specification
https://standards.ieee.org/ieee/754/6210/
See Also
BigInteger, MathContext, RoundingMode, java.util.SortedMap, java.util.SortedSet

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static volatile BigInteger[]
private static final int
private static final int
private static final long
private static final double[]
DOUBLE_10_POW

Powers of 10 which can be represented exactly in double.

private static final BigInteger[]
FIVE_TO_2_TO

FIVE_TO_2_TO[n] == 5^(2^n)

private static final float[]
FLOAT_10_POW

Powers of 10 which can be represented exactly in float.

private static final long
private static final long
pack-priv static final long
INFLATED

Sentinel value for intCompact indicating the significand information is only available from intVal.

private static final BigInteger
private final transient long
intCompact

If the absolute value of the significand of this BigDecimal is less than or equal to Long.MAX_VALUE, the value can be compactly stored in this field and used in computations.

private final BigInteger
intVal

The unscaled value of this BigDecimal, as returned by unscaledValue.

private static final JavaLangAccess
private static final double
L

private static final double
private static final long[]
private static final long[][]
private static final int
public static final BigDecimal
ONE

The value 1, with a scale of 0.

private static final int
private static final int
private transient int
precision

The number of decimal digits in this BigDecimal, or 0 if the number of digits are not known (lookaside information).

private static final int
private static final int
private static final int
private static final int
public static final int
ROUND_CEILING

Deprecated since 9. Use RoundingMode#CEILING instead.
Rounding mode to round towards positive infinity.
public static final int
ROUND_DOWN

Deprecated since 9. Use RoundingMode#DOWN instead.
Rounding mode to round towards zero.
public static final int
ROUND_FLOOR

Deprecated since 9. Use RoundingMode#FLOOR instead.
Rounding mode to round towards negative infinity.
public static final int
ROUND_HALF_DOWN

Deprecated since 9. Use RoundingMode#HALF_DOWN instead.
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
public static final int
ROUND_HALF_EVEN

Deprecated since 9. Use RoundingMode#HALF_EVEN instead.
Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
public static final int
ROUND_HALF_UP

Deprecated since 9. Use RoundingMode#HALF_UP instead.
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
public static final int
ROUND_UNNECESSARY

Deprecated since 9. Use RoundingMode#UNNECESSARY instead.
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
public static final int
ROUND_UP

Deprecated since 9. Use RoundingMode#UP instead.
Rounding mode to round away from zero.
private final int
scale

The scale of this BigDecimal, as returned by scale().

private static final long
private transient String
stringCache

Used to store the canonical string representation, if computed.

public static final BigDecimal
TEN

The value 10, with a scale of 0.

private static final long[]
public static final BigDecimal
TWO

The value 2, with a scale of 0.

public static final BigDecimal
ZERO

The value 0, with a scale of 0.

private static final BigDecimal[]
private static final BigDecimal[]

Constructor Summary

AccessConstructor and Description
pack-priv
BigDecimal(BigInteger intVal, long val, int scale, int prec)

Trusted package private constructor.

public
BigDecimal(char[]
char array that is the source of characters.
in
,
int
first character in the array to inspect.
offset
,
int
number of characters to consider.
len
)

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified.

public
BigDecimal(char[]
char array that is the source of characters.
in
,
int
first character in the array to inspect.
offset
,
int
number of characters to consider.
len
,
MathContext
the context to use.
mc
)

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified and with rounding according to the context settings.

public
BigDecimal(char[]
char array that is the source of characters.
in
)

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor.

public
BigDecimal(char[]
char array that is the source of characters.
in
,
MathContext
the context to use.
mc
)

Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor and with rounding according to the context settings.

public
BigDecimal(String
String representation of BigDecimal.
val
)

Translates the string representation of a BigDecimal into a BigDecimal.

public
BigDecimal(String
string representation of a BigDecimal.
val
,
MathContext
the context to use.
mc
)

Translates the string representation of a BigDecimal into a BigDecimal, accepting the same strings as the BigDecimal(String) constructor, with rounding according to the context settings.

public
BigDecimal(double
double value to be converted to BigDecimal.
val
)

Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value.

public
BigDecimal(double
double value to be converted to BigDecimal.
val
,
MathContext
the context to use.
mc
)

Translates a double into a BigDecimal, with rounding according to the context settings.

public
BigDecimal(BigInteger
BigInteger value to be converted to BigDecimal.
val
)

Translates a BigInteger into a BigDecimal.

public
BigDecimal(BigInteger
BigInteger value to be converted to BigDecimal.
val
,
MathContext
the context to use.
mc
)

Translates a BigInteger into a BigDecimal rounding according to the context settings.

public
BigDecimal(BigInteger
unscaled value of the BigDecimal.
unscaledVal
,
int
scale of the BigDecimal.
scale
)

Translates a BigInteger unscaled value and an int scale into a BigDecimal.

public
BigDecimal(BigInteger
unscaled value of the BigDecimal.
unscaledVal
,
int
scale of the BigDecimal.
scale
,
MathContext
the context to use.
mc
)

Translates a BigInteger unscaled value and an int scale into a BigDecimal, with rounding according to the context settings.

public
BigDecimal(int
int value to be converted to BigDecimal.
val
)

Translates an int into a BigDecimal.

public
BigDecimal(int
int value to be converted to BigDecimal.
val
,
MathContext
the context to use.
mc
)

Translates an int into a BigDecimal, with rounding according to the context settings.

public
BigDecimal(long
long value to be converted to BigDecimal.
val
)

Translates a long into a BigDecimal.

public
BigDecimal(long
long value to be converted to BigDecimal.
val
,
MathContext
the context to use.
mc
)

Translates a long into a BigDecimal, with rounding according to the context settings.

Method Summary

Modifier and TypeMethod and Description
public BigDecimal

Returns:

abs(this)
abs
()

Returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().

public BigDecimal

Returns:

abs(this), rounded as necessary.
abs
(MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.

public BigDecimal

Returns:

this + augend
add
(BigDecimal
value to be added to this BigDecimal.
augend
)

Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).

public BigDecimal

Returns:

this + augend, rounded as necessary.
add
(BigDecimal
value to be added to this BigDecimal.
augend
,
MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (this + augend), with rounding according to the context settings.

private static long
add(long xs, long ys)

private static BigDecimal
add(long xs, long ys, int scale)

private static BigDecimal
add(final long xs, int scale1, final long ys, int scale2)

private static BigDecimal
add(final long xs, int scale1, BigInteger snd, int scale2)

private static BigDecimal
add(BigInteger fst, int scale1, BigInteger snd, int scale2)

private BigDecimal

Returns:

a BigDecimal numerically equivalent to this, whose precision does not exceed maxPrecision and whose scale is the closest to preferredScale.
adjustToPreferredScale
(int
the scale to reach
preferredScale
,
int
the largest precision the result can have. maxPrecision == 0 means that the result can have arbitrary precision.
maxPrecision
)

Assumes (precision() <= maxPrecision || maxPrecision == 0) && this != 0.

private BigDecimal
audit()

Check internal invariants of this BigDecimal.

private static int

Returns:

the length of the unscaled value, in decimal digits
bigDigitLength
(BigInteger
the BigInteger
b
)

Returns the length of the absolute value of a BigInteger, in decimal digits.

private BigInteger
bigMultiplyPowerTen(int n)

Compute this * 10 ^ n.

private static BigInteger
bigMultiplyPowerTen(long value, int n)

private static BigInteger
private static BigInteger

Returns:

a BigInteger with the value (10n)
bigTenToThe
(int
the power of ten to be returned (>=0)
n
)

Return 10 to the power n, as a BigInteger.

public byte

Returns:

this BigDecimal converted to a byte.
byteValueExact
()

Converts this BigDecimal to a byte, checking for lost information.

private int

Returns:

validated scale as an int.
checkScale
(long
The new scale.
val
)

Check a scale for Underflow or Overflow.

private static int
checkScale(long intCompact, long val)

private static int
checkScale(BigInteger intVal, long val)

private static int
private static boolean
commonNeedIncrement(int roundingMode, int qsign, int cmpFracHalf, boolean oddQuot)

Shared logic of need increment computation.

private static long
compactValFor(BigInteger b)

Returns the compact value for given BigInteger, or INFLATED if too big.

private int
compareMagnitude(BigDecimal val)

Version of compareTo that ignores sign.

private static int
compareMagnitudeNormalized(long xs, int xscale, long ys, int yscale)

private static int
compareMagnitudeNormalized(long xs, int xscale, BigInteger ys, int yscale)

private static int
compareMagnitudeNormalized(BigInteger xs, int xscale, BigInteger ys, int yscale)

public int

Returns:

-1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
compareTo
(BigDecimal
BigDecimal to which this BigDecimal is to be compared.
val
)

Implements java.lang.Comparable.compareTo.

Compares this BigDecimal numerically with the specified BigDecimal.

private static BigDecimal

Returns:

new BigDecimal with a scale possibly reduced to be closed to the preferred scale.
createAndStripZerosToMatchScale
(BigInteger intVal, int scale, long preferredScale)

Remove insignificant trailing zeros from this BigInteger value until the preferred scale is reached or no more zeros can be removed.

private static BigDecimal

Returns:

new BigDecimal with a scale possibly reduced to be closed to the preferred scale.
createAndStripZerosToMatchScale
(long compactVal, int scale, long preferredScale)

Remove insignificant trailing zeros from this long value until the preferred scale is reached or no more zeros can be removed.

private static int
digitLengthLower(BigInteger b)

Returns: an integer r such that 10^(r-1) <= abs(b) < 10^(r+1).

public BigDecimal

Returns:

this / divisor
divide
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
,
int
scale of the BigDecimal quotient to be returned.
scale
,
int
rounding mode to apply.
roundingMode
)
Deprecated since 9. The method divide(BigDecimal, int, RoundingMode) should be used in preference to this legacy method.

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.

public BigDecimal

Returns:

this / divisor
divide
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
,
int
scale of the BigDecimal quotient to be returned.
scale
,
RoundingMode
rounding mode to apply.
roundingMode
)

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified.

public BigDecimal

Returns:

this / divisor
divide
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
,
int
rounding mode to apply.
roundingMode
)
Deprecated since 9. The method divide(BigDecimal, RoundingMode) should be used in preference to this legacy method.

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().

public BigDecimal

Returns:

this / divisor
divide
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
,
RoundingMode
rounding mode to apply.
roundingMode
)

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale().

public BigDecimal

Returns:

this / divisor
divide
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
)

Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.

public BigDecimal

Returns:

this / divisor, rounded as necessary.
divide
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
,
MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (this / divisor), with rounding according to the context settings.

private static BigDecimal
divide(final long xs, int xscale, final long ys, int yscale, long preferredScale, MathContext mc)

Returns a BigDecimal whose value is (xs / ys), with rounding according to the context settings.

private static BigDecimal
divide(BigInteger xs, int xscale, long ys, int yscale, long preferredScale, MathContext mc)

Returns a BigDecimal whose value is (xs / ys), with rounding according to the context settings.

private static BigDecimal
divide(long xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc)

Returns a BigDecimal whose value is (xs / ys), with rounding according to the context settings.

private static BigDecimal
divide(BigInteger xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc)

Returns a BigDecimal whose value is (xs / ys), with rounding according to the context settings.

private static BigDecimal
divide(long dividend, int dividendScale, long divisor, int divisorScale, int scale, int roundingMode)

private static BigDecimal
divide(BigInteger dividend, int dividendScale, long divisor, int divisorScale, int scale, int roundingMode)

private static BigDecimal
divide(long dividend, int dividendScale, BigInteger divisor, int divisorScale, int scale, int roundingMode)

private static BigDecimal
divide(BigInteger dividend, int dividendScale, BigInteger divisor, int divisorScale, int scale, int roundingMode)

public BigDecimal[]

Returns:

a two element BigDecimal array: the quotient (the result of divideToIntegralValue) is the initial element and the remainder is the final element.
divideAndRemainder
(BigDecimal
value by which this BigDecimal is to be divided, and the remainder computed.
divisor
)

Returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands.

public BigDecimal[]

Returns:

a two element BigDecimal array: the quotient (the result of divideToIntegralValue) is the initial element and the remainder is the final element.
divideAndRemainder
(BigDecimal
value by which this BigDecimal is to be divided, and the remainder computed.
divisor
,
MathContext
the context to use.
mc
)

Returns a two-element BigDecimal array containing the result of divideToIntegralValue followed by the result of remainder on the two operands calculated with rounding according to the context settings.

private static BigDecimal
divideAndRound(long ldividend, long ldivisor, int scale, int roundingMode, int preferredScale)

Internally used for division operation for division long by long.

private static long
divideAndRound(long ldividend, long ldivisor, int roundingMode)

Divides long by long and do rounding based on the passed in roundingMode.

private static BigInteger
divideAndRound(BigInteger bdividend, long ldivisor, int roundingMode)

Divides BigInteger value by long value and do rounding based on the passed in roundingMode.

private static BigDecimal
divideAndRound(BigInteger bdividend, long ldivisor, int scale, int roundingMode, int preferredScale)

Internally used for division operation for division BigInteger by long.

private static BigInteger
divideAndRound(BigInteger bdividend, BigInteger bdivisor, int roundingMode)

Divides BigInteger value by BigInteger value and do rounding based on the passed in roundingMode.

private static BigDecimal
divideAndRound(BigInteger bdividend, BigInteger bdivisor, int scale, int roundingMode, int preferredScale)

Internally used for division operation for division BigInteger by BigInteger.

private static BigDecimal
divideAndRound128(final long dividendHi, final long dividendLo, long divisor, int sign, int scale, int roundingMode, int preferredScale)

private static BigInteger
divideAndRoundByTenPow(BigInteger intVal, int tenPow, int roundingMode)

private static BigDecimal
divideSmallFastPath(final long xs, int xscale, final long ys, int yscale, long preferredScale, MathContext mc)

Returns a BigDecimal whose value is (xs / ys), with rounding according to the context settings.

public BigDecimal

Returns:

The integer part of this / divisor.
divideToIntegralValue
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
)

Returns a BigDecimal whose value is the integer part of the quotient (this / divisor) rounded down.

public BigDecimal

Returns:

The integer part of this / divisor.
divideToIntegralValue
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
,
MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is the integer part of (this / divisor).

private static BigDecimal

Returns:

a BigDecimal rounded according to the MathContext settings. May return value, if no rounding needed.
doRound
(BigDecimal
the value to be rounded
val
,
MathContext
the context to use.
mc
)

Returns a BigDecimal rounded according to the MathContext settings; If rounding is needed a new BigDecimal is created and returned.

private static BigDecimal
doRound(long compactVal, int scale, MathContext mc)

private static BigDecimal
doRound(BigInteger intVal, int scale, MathContext mc)

private static BigDecimal
doRound128(long hi, long lo, int sign, int scale, MathContext mc)

rounds 128-bit value according MathContext returns null if result can't be repsented as compact BigDecimal.

public double

Returns:

this BigDecimal converted to a double.
doubleValue
()

Implements abstract java.lang.Number.doubleValue.

Converts this BigDecimal to a double.

public boolean

Returns:

true if and only if the specified Object is a BigDecimal whose value and scale are equal to this BigDecimal's.
equals
(Object
Object to which this BigDecimal is to be compared.
x
)

Overrides java.lang.Object.equals.

Compares this BigDecimal with the specified Object for equality.

private static BigInteger

Returns:

a BigDecimal with the value (10n) and in the meantime, the BIG_TEN_POWERS_TABLE array gets expanded to the size greater than n.
expandBigIntegerTenPowers
(int
the power of ten to be returned (>=0)
n
)

Expand the BIG_TEN_POWERS_TABLE array to contain at least 10**n.

private static BigInteger
fiveToTwoToThe(int
a non-negative integer
n
)

Returns: 5^(2^n)

public float

Returns:

this BigDecimal converted to a float.
floatValue
()

Implements abstract java.lang.Number.floatValue.

Converts this BigDecimal to a float.

private boolean
fractionOnly()

Return true if a nonzero BigDecimal has an absolute value less than one; i.e. only has fraction digits.

private double
private float
private static String
getValueString(int signum, String intString, int scale)

public int

Returns:

hash code for this BigDecimal.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the hash code for this BigDecimal.

private BigInteger
inflated()

Returns appropriate BigInteger from intVal field if intVal is null, i.e. the compact representation is in use.

public int

Returns:

this BigDecimal converted to an int.
intValue
()

Implements abstract java.lang.Number.intValue.

Converts this BigDecimal to an int.

public int

Returns:

this BigDecimal converted to an int.
intValueExact
()

Converts this BigDecimal to an int, checking for lost information.

private static boolean
pack-priv boolean
isInteger()

Returns: true if and only if this == this.toBigInteger()

private boolean
private String

Returns:

string with canonical string representation of this BigDecimal
layoutChars
(boolean
true for Scientific exponential notation; false for Engineering
sci
)

Lay out this BigDecimal into a char[] array.

private static int
longCompareMagnitude(long x, long y)

pack-priv static int

Returns:

the length of the unscaled value, in decimal digits.
longDigitLength
(long
the long
x
)

Returns the length of the absolute value of a long, in decimal digits.

private static boolean
longLongCompareMagnitude(long hi0, long lo0, long hi1, long lo1)

private static long
longMultiplyPowerTen(long val, int n)

Compute val * 10 ^ n; return this product if it is representable as a long, INFLATED otherwise.

public long

Returns:

this BigDecimal converted to a long.
longValue
()

Implements abstract java.lang.Number.longValue.

Converts this BigDecimal to a long.

public long

Returns:

this BigDecimal converted to a long.
longValueExact
()

Converts this BigDecimal to a long, checking for lost information.

private static long
make64(long hi, long lo)

private static void
matchScale(BigDecimal[]
array of two elements referring to the two BigDecimals to be aligned.
val
)

Match the scales of two BigDecimals to align their least significant digits.

public BigDecimal

Returns:

the BigDecimal whose value is the greater of this BigDecimal and val. If they are equal, as defined by the compareTo method, this is returned.
max
(BigDecimal
value with which the maximum is to be computed.
val
)

Returns the maximum of this BigDecimal and val.

public BigDecimal

Returns:

the BigDecimal whose value is the lesser of this BigDecimal and val. If they are equal, as defined by the compareTo method, this is returned.
min
(BigDecimal
value with which the minimum is to be computed.
val
)

Returns the minimum of this BigDecimal and val.

public BigDecimal

Returns:

a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.
movePointLeft
(int
number of places to move the decimal point to the left.
n
)

Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the left.

public BigDecimal

Returns:

a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.
movePointRight
(int
number of places to move the decimal point to the right.
n
)

Returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right.

private static long
mulsub(long u1, long u0, final long v1, final long v0, long q0)

public BigDecimal

Returns:

this * multiplicand
multiply
(BigDecimal
value to be multiplied by this BigDecimal.
multiplicand
)

Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).

public BigDecimal

Returns:

this * multiplicand, rounded as necessary.
multiply
(BigDecimal
value to be multiplied by this BigDecimal.
multiplicand
,
MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (this × multiplicand), with rounding according to the context settings.

private static long
multiply(long x, long y)

private static BigDecimal
multiply(long x, long y, int scale)

private static BigDecimal
multiply(long x, BigInteger y, int scale)

private static BigDecimal
multiply(BigInteger x, BigInteger y, int scale)

private static BigDecimal
multiplyAndRound(long x, long y, int scale, MathContext mc)

Multiplies two long values and rounds according MathContext

private static BigDecimal
multiplyAndRound(long x, BigInteger y, int scale, MathContext mc)

private static BigDecimal
private static BigDecimal
multiplyDivideAndRound(long dividend0, long dividend1, long divisor, int scale, int roundingMode, int preferredScale)

private static boolean
needIncrement(long ldivisor, int roundingMode, int qsign, long q, long r)

Tests if quotient has to be incremented according the roundingMode

private static boolean
needIncrement(long ldivisor, int roundingMode, int qsign, MutableBigInteger mq, long r)

Tests if quotient has to be incremented according the roundingMode

private static boolean
needIncrement(MutableBigInteger mdivisor, int roundingMode, int qsign, MutableBigInteger mq, MutableBigInteger mr)

Tests if quotient has to be incremented according the roundingMode

public BigDecimal

Returns:

-this.
negate
()

Returns a BigDecimal whose value is (-this), and whose scale is this.scale().

public BigDecimal

Returns:

-this, rounded as necessary.
negate
(MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (-this), with rounding according to the context settings.

private static long
parseExp(char[] in, int offset, int len)

public BigDecimal

Returns:

this.
plus
()

Returns a BigDecimal whose value is (+this), and whose scale is this.scale().

public BigDecimal

Returns:

this, rounded as necessary. A zero result will have a scale of 0.
plus
(MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (+this), with rounding according to the context settings.

public BigDecimal

Returns:

thisn
pow
(int
power to raise this BigDecimal to.
n
)

Returns a BigDecimal whose value is (thisn), The power is computed exactly, to unlimited precision.

public BigDecimal

Returns:

thisn using the ANSI standard X3.274-1996 algorithm
pow
(int
power to raise this BigDecimal to.
n
,
MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (thisn).

private BigDecimal[]
preAlign(BigDecimal lhs, BigDecimal augend, long padding, MathContext mc)

Returns an array of length two, the sum of whose entries is equal to the rounded sum of the BigDecimal arguments.

public int

Returns:

the precision of this BigDecimal.
precision
()

Returns the precision of this BigDecimal.

private static int
precision(long hi, long lo)

private void
readObject(ObjectInputStream
the stream being read.
s
)

Reconstitute the BigDecimal instance from a stream (that is, deserialize it).

private void
readObjectNoData()

Serialization without data not supported for this class.

public BigDecimal

Returns:

this % divisor.
remainder
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
)

Returns a BigDecimal whose value is (this % divisor).

public BigDecimal

Returns:

this % divisor, rounded as necessary.
remainder
(BigDecimal
value by which this BigDecimal is to be divided.
divisor
,
MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (this % divisor), with rounding according to the context settings.

public BigDecimal

Returns:

the nth root of this.
rootn
(int
the root degree
n
,
MathContext
the context to use.
mc
)

Returns an approximation to the nth root of this with rounding according to the context settings.

private boolean
rootnResultAssertions(BigDecimal result, MathContext mc, int n)

For nonzero values, check numerical correctness properties of the computed result for the chosen rounding mode.

public BigDecimal

Returns:

a BigDecimal rounded according to the MathContext settings.
round
(MathContext
the context to use.
mc
)

Returns a BigDecimal rounded according to the MathContext settings.

private static BigDecimal
roundedTenPower(int qsign, int raise, int scale, int preferredScale)

private static int
saturateLong(long s)

public int

Returns:

the scale of this BigDecimal.
scale
()

Returns the scale of this BigDecimal.

public BigDecimal

Returns:

a BigDecimal whose numerical value is equal to (this * 10n)
scaleByPowerOfTen
(int
the exponent power of ten to scale by
n
)

Returns a BigDecimal whose numerical value is equal to (this * 10n).

pack-priv static BigDecimal
scaledTenPow(int n, int sign, int scale)

public BigDecimal

Returns:

a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
setScale
(int
scale of the BigDecimal value to be returned.
newScale
,
RoundingMode
The rounding mode to apply.
roundingMode
)

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.

public BigDecimal

Returns:

a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
setScale
(int
scale of the BigDecimal value to be returned.
newScale
,
int
The rounding mode to apply.
roundingMode
)
Deprecated since 9. The method setScale(int, RoundingMode) should be used in preference to this legacy method.

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.

public BigDecimal

Returns:

a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
setScale
(int
scale of the BigDecimal value to be returned.
newScale
)

Returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal's.

public short

Returns:

this BigDecimal converted to a short.
shortValueExact
()

Converts this BigDecimal to a short, checking for lost information.

public int

Returns:

-1, 0, or 1 as the value of this BigDecimal is negative, zero, or positive.
signum
()

Returns the signum function of this BigDecimal.

public BigDecimal

Returns:

the square root of this.
sqrt
(MathContext
the context to use.
mc
)

Returns an approximation to the square root of this with rounding according to the context settings.

public BigDecimal

Returns:

a numerically equal BigDecimal with any trailing zeros removed.
stripTrailingZeros
()

Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.

private static BigDecimal
stripZerosToMatchScale(BigInteger intVal, long intCompact, int scale, long preferredScale)

Assumes intVal != 0 && intCompact != 0.

public BigDecimal

Returns:

this - subtrahend
subtract
(BigDecimal
value to be subtracted from this BigDecimal.
subtrahend
)

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

public BigDecimal

Returns:

this - subtrahend, rounded as necessary.
subtract
(BigDecimal
value to be subtracted from this BigDecimal.
subtrahend
,
MathContext
the context to use.
mc
)

Returns a BigDecimal whose value is (this - subtrahend), with rounding according to the context settings.

public BigInteger

Returns:

this BigDecimal converted to a BigInteger.
toBigInteger
()

Converts this BigDecimal to a BigInteger.

public BigInteger

Returns:

this BigDecimal converted to a BigInteger.
toBigIntegerExact
()

Converts this BigDecimal to a BigInteger, checking for lost information.

public String

Returns:

string representation of this BigDecimal, using engineering notation if an exponent is needed.
toEngineeringString
()

Returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.

public String

Returns:

a string representation of this BigDecimal without an exponent field.
toPlainString
()

Returns a string representation of this BigDecimal without an exponent field.

private static BigInteger
toStrictBigInteger(BigInteger val)

Accept no subclasses.

public String

Returns:

string representation of this BigDecimal.
toString
()

Overrides java.lang.Object.toString.

Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.

public BigDecimal

Returns:

the size of an ulp of this
ulp
()

Returns the size of an ulp, a unit in the last place, of this BigDecimal.

public BigInteger

Returns:

the unscaled value of this BigDecimal.
unscaledValue
()

Returns a BigInteger whose value is the unscaled value of this BigDecimal.

private static boolean
unsignedLongCompare(long one, long two)

private static boolean
unsignedLongCompareEq(long one, long two)

public static BigDecimal

Returns:

a BigDecimal whose value is (unscaledVal × 10-scale).
valueOf
(long
unscaled value of the BigDecimal.
unscaledVal
,
int
scale of the BigDecimal.
scale
)

Translates a long unscaled value and an int scale into a BigDecimal.

public static BigDecimal

Returns:

a BigDecimal whose value is val.
valueOf
(long
value of the BigDecimal.
val
)

Translates a long value into a BigDecimal with a scale of zero.

pack-priv static BigDecimal
valueOf(long unscaledVal, int scale, int prec)

pack-priv static BigDecimal
valueOf(BigInteger intVal, int scale, int prec)

public static BigDecimal

Returns:

a BigDecimal whose value is equal to or approximately equal to the value of val.
valueOf
(double
double to convert to a BigDecimal.
val
)

Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double#toString(double) method.

private void
writeObject(ObjectOutputStream
the stream to serialize to.
s
)

Serialize this BigDecimal to the stream in question

pack-priv static BigDecimal
zeroValueOf(int scale)

Inherited from java.lang.Number:
byteValueshortValue