GeezerVison is showing docs.oracle.com — BigDecimal (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.math
Class BigDecimal
-
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
static BigDecimal |
ONE |
The value 1, with a scale of 0.
|
static int |
ROUND_CEILING |
Rounding mode to round towards positive infinity.
|
static int |
ROUND_DOWN |
Rounding mode to round towards zero.
|
static int |
ROUND_FLOOR |
Rounding mode to round towards negative infinity.
|
static int |
ROUND_HALF_DOWN |
Rounding mode to round towards "nearest neighbor"
unless both neighbors are equidistant, in which case round
down.
|
static int |
ROUND_HALF_EVEN |
Rounding mode to round towards the "nearest neighbor"
unless both neighbors are equidistant, in which case, round
towards the even neighbor.
|
static int |
ROUND_HALF_UP |
Rounding mode to round towards "nearest neighbor"
unless both neighbors are equidistant, in which case round up.
|
static int |
ROUND_UNNECESSARY |
Rounding mode to assert that the requested operation has an exact
result, hence no rounding is necessary.
|
static int |
ROUND_UP |
Rounding mode to round away from zero.
|
static BigDecimal |
TEN |
The value 10, with a scale of 0.
|
static BigDecimal |
ZERO |
The value 0, with a scale of 0.
|
-
Constructor Summary
Constructors
| Constructor |
Description |
BigDecimal(BigInteger val) |
Translates a BigInteger into a BigDecimal.
|
BigDecimal(BigInteger unscaledVal,
int scale) |
Translates a BigInteger unscaled value and an
int scale into a BigDecimal.
|
BigDecimal(BigInteger unscaledVal,
int scale,
MathContext mc) |
Translates a BigInteger unscaled value and an
int scale into a BigDecimal, with rounding
according to the context settings.
|
BigDecimal(BigInteger val,
MathContext mc) |
Translates a BigInteger into a BigDecimal
rounding according to the context settings.
|
BigDecimal(char[] in) |
Translates a character array representation of a
BigDecimal into a BigDecimal, accepting the
same sequence of characters as the BigDecimal(String)
constructor.
|
BigDecimal(char[] in,
int offset,
int 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.
|
BigDecimal(char[] in,
int offset,
int len,
MathContext 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.
|
BigDecimal(char[] in,
MathContext 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.
|
BigDecimal(double val) |
Translates a double into a BigDecimal which
is the exact decimal representation of the double's
binary floating-point value.
|
BigDecimal(double val,
MathContext mc) |
Translates a double into a BigDecimal, with
rounding according to the context settings.
|
BigDecimal(int val) |
Translates an int into a BigDecimal.
|
BigDecimal(int val,
MathContext mc) |
Translates an int into a BigDecimal, with
rounding according to the context settings.
|
BigDecimal(long val) |
Translates a long into a BigDecimal.
|
BigDecimal(long val,
MathContext mc) |
Translates a long into a BigDecimal, with
rounding according to the context settings.
|
BigDecimal(String val) |
Translates the string representation of a BigDecimal
into a BigDecimal.
|
BigDecimal(String val,
MathContext 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.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
BigDecimal |
abs() |
Returns a BigDecimal whose value is the absolute value
of this BigDecimal, and whose scale is
this.scale().
|
BigDecimal |
abs(MathContext mc) |
Returns a BigDecimal whose value is the absolute value
of this BigDecimal, with rounding according to the
context settings.
|
BigDecimal |
add(BigDecimal augend) |
Returns a BigDecimal whose value is (this +
augend), and whose scale is max(this.scale(),
augend.scale()).
|
BigDecimal |
add(BigDecimal augend,
MathContext mc) |
Returns a BigDecimal whose value is (this + augend),
with rounding according to the context settings.
|
byte |
byteValueExact() |
Converts this BigDecimal to a byte, checking
for lost information.
|
int |
compareTo(BigDecimal val) |
Compares this BigDecimal with the specified
BigDecimal.
|
BigDecimal |
divide(BigDecimal 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.
|
BigDecimal |
divide(BigDecimal divisor,
int roundingMode) |
Returns a BigDecimal whose value is (this /
divisor), and whose scale is this.scale().
|
BigDecimal |
divide(BigDecimal divisor,
int scale,
int roundingMode) |
Returns a BigDecimal whose value is (this /
divisor), and whose scale is as specified.
|
BigDecimal |
divide(BigDecimal divisor,
int scale,
RoundingMode roundingMode) |
Returns a BigDecimal whose value is (this /
divisor), and whose scale is as specified.
|
BigDecimal |
divide(BigDecimal divisor,
MathContext mc) |
Returns a BigDecimal whose value is (this /
divisor), with rounding according to the context settings.
|
BigDecimal |
divide(BigDecimal divisor,
RoundingMode roundingMode) |
Returns a BigDecimal whose value is (this /
divisor), and whose scale is this.scale().
|
BigDecimal[] |
divideAndRemainder(BigDecimal divisor) |
Returns a two-element BigDecimal array containing the
result of divideToIntegralValue followed by the result of
remainder on the two operands.
|
BigDecimal[] |
divideAndRemainder(BigDecimal divisor,
MathContext 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.
|
BigDecimal |
divideToIntegralValue(BigDecimal divisor) |
Returns a BigDecimal whose value is the integer part
of the quotient (this / divisor) rounded down.
|
BigDecimal |
divideToIntegralValue(BigDecimal divisor,
MathContext mc) |
Returns a BigDecimal whose value is the integer part
of (this / divisor).
|
double |
doubleValue() |
Converts this BigDecimal to a double.
|
boolean |
equals(Object x) |
Compares this BigDecimal with the specified
Object for equality.
|
float |
floatValue() |
Converts this BigDecimal to a float.
|
int |
hashCode() |
Returns the hash code for this BigDecimal.
|
int |
intValue() |
Converts this BigDecimal to an int.
|
int |
intValueExact() |
Converts this BigDecimal to an int, checking
for lost information.
|
long |
longValue() |
Converts this BigDecimal to a long.
|
long |
longValueExact() |
Converts this BigDecimal to a long, checking
for lost information.
|
BigDecimal |
max(BigDecimal val) |
Returns the maximum of this BigDecimal and val.
|
BigDecimal |
min(BigDecimal val) |
Returns the minimum of this BigDecimal and
val.
|
BigDecimal |
movePointLeft(int n) |
Returns a BigDecimal which is equivalent to this one
with the decimal point moved n places to the left.
|
BigDecimal |
movePointRight(int n) |
Returns a BigDecimal which is equivalent to this one
with the decimal point moved n places to the right.
|
BigDecimal |
multiply(BigDecimal multiplicand) |
Returns a BigDecimal whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()).
|
BigDecimal |
multiply(BigDecimal multiplicand,
MathContext mc) |
Returns a BigDecimal whose value is (this ×
multiplicand), with rounding according to the context settings.
|
BigDecimal |
negate() |
Returns a BigDecimal whose value is (-this),
and whose scale is this.scale().
|
BigDecimal |
negate(MathContext mc) |
Returns a BigDecimal whose value is (-this),
with rounding according to the context settings.
|
BigDecimal |
plus() |
Returns a BigDecimal whose value is (+this), and whose
scale is this.scale().
|
BigDecimal |
plus(MathContext mc) |
Returns a BigDecimal whose value is (+this),
with rounding according to the context settings.
|
BigDecimal |
pow(int n) |
Returns a BigDecimal whose value is
(thisn), The power is computed exactly, to
unlimited precision.
|
BigDecimal |
pow(int n,
MathContext mc) |
Returns a BigDecimal whose value is
(thisn).
|
int |
precision() |
Returns the precision of this BigDecimal.
|
BigDecimal |
remainder(BigDecimal divisor) |
Returns a BigDecimal whose value is (this % divisor).
|
BigDecimal |
remainder(BigDecimal divisor,
MathContext mc) |
Returns a BigDecimal whose value is (this %
divisor), with rounding according to the context settings.
|
BigDecimal |
round(MathContext mc) |
Returns a BigDecimal rounded according to the
MathContext settings.
|
int |
scale() |
Returns the scale of this BigDecimal.
|
BigDecimal |
scaleByPowerOfTen(int n) |
Returns a BigDecimal whose numerical value is equal to
(this * 10n).
|
BigDecimal |
setScale(int newScale) |
Returns a BigDecimal whose scale is the specified
value, and whose value is numerically equal to this
BigDecimal's.
|
BigDecimal |
setScale(int newScale,
int 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.
|
BigDecimal |
setScale(int newScale,
RoundingMode 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.
|
short |
shortValueExact() |
Converts this BigDecimal to a short, checking
for lost information.
|
int |
signum() |
Returns the signum function of this BigDecimal.
|
BigDecimal |
stripTrailingZeros() |
Returns a BigDecimal which is numerically equal to
this one but with any trailing zeros removed from the
representation.
|
BigDecimal |
subtract(BigDecimal subtrahend) |
Returns a BigDecimal whose value is (this -
subtrahend), and whose scale is max(this.scale(),
subtrahend.scale()).
|
BigDecimal |
subtract(BigDecimal subtrahend,
MathContext mc) |
Returns a BigDecimal whose value is (this - subtrahend),
with rounding according to the context settings.
|
BigInteger |
toBigInteger() |
Converts this BigDecimal to a BigInteger.
|
BigInteger |
toBigIntegerExact() |
Converts this BigDecimal to a BigInteger,
checking for lost information.
|
String |
toEngineeringString() |
Returns a string representation of this BigDecimal,
using engineering notation if an exponent is needed.
|
String |
toPlainString() |
Returns a string representation of this BigDecimal
without an exponent field.
|
String |
toString() |
Returns the string representation of this BigDecimal,
using scientific notation if an exponent is needed.
|
BigDecimal |
ulp() |
Returns the size of an ulp, a unit in the last place, of this
BigDecimal.
|
BigInteger |
unscaledValue() |
Returns a BigInteger whose value is the unscaled
value of this BigDecimal.
|
static BigDecimal |
valueOf(double val) |
Translates a double into a BigDecimal, using
the double's canonical string representation provided
by the Double.toString(double) method.
|
static BigDecimal |
valueOf(long val) |
Translates a long value into a BigDecimal
with a scale of zero.
|
static BigDecimal |
valueOf(long unscaledVal,
int scale) |
Translates a long unscaled value and an
int scale into a BigDecimal.
|
-
-
Field Detail
-
ROUND_UP
public static final int ROUND_UP
Rounding mode to round away from zero. Always increments the
digit prior to a nonzero discarded fraction. Note that this rounding
mode never decreases the magnitude of the calculated value.
- See Also:
- Constant Field Values
-
ROUND_DOWN
public static final int ROUND_DOWN
Rounding mode to round towards zero. Never increments the digit
prior to a discarded fraction (i.e., truncates). Note that this
rounding mode never increases the magnitude of the calculated value.
- See Also:
- Constant Field Values
-
ROUND_CEILING
public static final int ROUND_CEILING
Rounding mode to round towards positive infinity. If the
BigDecimal is positive, behaves as for
ROUND_UP; if negative, behaves as for
ROUND_DOWN. Note that this rounding mode never
decreases the calculated value.
- See Also:
- Constant Field Values
-
ROUND_FLOOR
public static final int ROUND_FLOOR
Rounding mode to round towards negative infinity. If the
BigDecimal is positive, behave as for
ROUND_DOWN; if negative, behave as for
ROUND_UP. Note that this rounding mode never
increases the calculated value.
- See Also:
- Constant Field Values
-
ROUND_HALF_UP
public static final int ROUND_HALF_UP
Rounding mode to round towards "nearest neighbor"
unless both neighbors are equidistant, in which case round up.
Behaves as for ROUND_UP if the discarded fraction is
≥ 0.5; otherwise, behaves as for ROUND_DOWN. Note
that this is the rounding mode that most of us were taught in
grade school.
- See Also:
- Constant Field Values
-
ROUND_HALF_DOWN
public static final int ROUND_HALF_DOWN
Rounding mode to round towards "nearest neighbor"
unless both neighbors are equidistant, in which case round
down. Behaves as for ROUND_UP if the discarded
fraction is > 0.5; otherwise, behaves as for
ROUND_DOWN.
- See Also:
- Constant Field Values
-
ROUND_HALF_EVEN
public static final int ROUND_HALF_EVEN
Rounding mode to round towards the "nearest neighbor"
unless both neighbors are equidistant, in which case, round
towards the even neighbor. Behaves as for
ROUND_HALF_UP if the digit to the left of the
discarded fraction is odd; behaves as for
ROUND_HALF_DOWN if it's even. Note that this is the
rounding mode that minimizes cumulative error when applied
repeatedly over a sequence of calculations.
- See Also:
- Constant Field Values
-
ROUND_UNNECESSARY
public static final int ROUND_UNNECESSARY
Rounding mode to assert that the requested operation has an exact
result, hence no rounding is necessary. If this rounding mode is
specified on an operation that yields an inexact result, an
ArithmeticException is thrown.
- See Also:
- Constant Field Values
-
Constructor Detail
-
BigDecimal
public BigDecimal(BigInteger val,
MathContext mc)
Translates a BigInteger into a BigDecimal
rounding according to the context settings. The scale of the
BigDecimal is zero.
- Parameters:
val - BigInteger value to be converted to
BigDecimal.
mc - the context to use.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
BigDecimal
public BigDecimal(BigInteger unscaledVal,
int scale)
Translates a BigInteger unscaled value and an
int scale into a BigDecimal. The value of
the BigDecimal is
(unscaledVal × 10-scale).
- Parameters:
unscaledVal - unscaled value of the BigDecimal.
scale - scale of the BigDecimal.
-
BigDecimal
public BigDecimal(BigInteger unscaledVal,
int scale,
MathContext mc)
Translates a BigInteger unscaled value and an
int scale into a BigDecimal, with rounding
according to the context settings. The value of the
BigDecimal is (unscaledVal ×
10-scale), rounded according to the
precision and rounding mode settings.
- Parameters:
unscaledVal - unscaled value of the BigDecimal.
scale - scale of the BigDecimal.
mc - the context to use.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
BigDecimal
public BigDecimal(int val,
MathContext mc)
Translates an int into a BigDecimal, with
rounding according to the context settings. The scale of the
BigDecimal, before any rounding, is zero.
- Parameters:
val - int value to be converted to BigDecimal.
mc - the context to use.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
BigDecimal
public BigDecimal(long val,
MathContext mc)
Translates a long into a BigDecimal, with
rounding according to the context settings. The scale of the
BigDecimal, before any rounding, is zero.
- Parameters:
val - long value to be converted to BigDecimal.
mc - the context to use.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
Method Detail
-
valueOf
public static BigDecimal valueOf(long unscaledVal,
int scale)
Translates a long unscaled value and an
int scale into a BigDecimal. This
"static factory method" is provided in preference to
a (long, int) constructor because it
allows for reuse of frequently used BigDecimal values..
- Parameters:
unscaledVal - unscaled value of the BigDecimal.
scale - scale of the BigDecimal.
- Returns:
- a
BigDecimal whose value is
(unscaledVal × 10-scale).
-
valueOf
public static BigDecimal valueOf(long val)
Translates a long value into a BigDecimal
with a scale of zero. This "static factory method"
is provided in preference to a (long) constructor
because it allows for reuse of frequently used
BigDecimal values.
- Parameters:
val - value of the BigDecimal.
- Returns:
- a
BigDecimal whose value is val.
-
add
public BigDecimal add(BigDecimal augend)
Returns a BigDecimal whose value is (this +
augend), and whose scale is max(this.scale(),
augend.scale()).
- Parameters:
augend - value to be added to this BigDecimal.
- Returns:
this + augend
-
add
public BigDecimal add(BigDecimal augend,
MathContext mc)
Returns a BigDecimal whose value is (this + augend),
with rounding according to the context settings.
If either number is zero and the precision setting is nonzero then
the other number, rounded if necessary, is used as the result.
- Parameters:
augend - value to be added to this BigDecimal.
mc - the context to use.
- Returns:
this + augend, rounded as necessary.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
subtract
public BigDecimal subtract(BigDecimal subtrahend)
Returns a BigDecimal whose value is (this -
subtrahend), and whose scale is max(this.scale(),
subtrahend.scale()).
- Parameters:
subtrahend - value to be subtracted from this BigDecimal.
- Returns:
this - subtrahend
-
subtract
public BigDecimal subtract(BigDecimal subtrahend,
MathContext mc)
Returns a BigDecimal whose value is (this - subtrahend),
with rounding according to the context settings.
If subtrahend is zero then this, rounded if necessary, is used as the
result. If this is zero then the result is subtrahend.negate(mc).
- Parameters:
subtrahend - value to be subtracted from this BigDecimal.
mc - the context to use.
- Returns:
this - subtrahend, rounded as necessary.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
multiply
public BigDecimal multiply(BigDecimal multiplicand)
Returns a BigDecimal whose value is (this ×
multiplicand), and whose scale is (this.scale() +
multiplicand.scale()).
- Parameters:
multiplicand - value to be multiplied by this BigDecimal.
- Returns:
this * multiplicand
-
multiply
public BigDecimal multiply(BigDecimal multiplicand,
MathContext mc)
Returns a BigDecimal whose value is (this ×
multiplicand), with rounding according to the context settings.
- Parameters:
multiplicand - value to be multiplied by this BigDecimal.
mc - the context to use.
- Returns:
this * multiplicand, rounded as necessary.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
divide
public BigDecimal divide(BigDecimal divisor,
int scale,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (this /
divisor), and whose scale is as specified. If rounding must
be performed to generate a result with the specified scale, the
specified rounding mode is applied.
- Parameters:
divisor - value by which this BigDecimal is to be divided.
scale - scale of the BigDecimal quotient to be returned.
roundingMode - rounding mode to apply.
- Returns:
this / divisor
- Throws:
ArithmeticException - if divisor is zero,
roundingMode==RoundingMode.UNNECESSARY and
the specified scale is insufficient to represent the result
of the division exactly.
- Since:
- 1.5
-
divide
public BigDecimal divide(BigDecimal divisor,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (this /
divisor), and whose scale is this.scale(). If
rounding must be performed to generate a result with the given
scale, the specified rounding mode is applied.
- Parameters:
divisor - value by which this BigDecimal is to be divided.
roundingMode - rounding mode to apply.
- Returns:
this / divisor
- Throws:
ArithmeticException - if divisor==0, or
roundingMode==RoundingMode.UNNECESSARY and
this.scale() is insufficient to represent the result
of the division exactly.
- Since:
- 1.5
-
divide
public BigDecimal divide(BigDecimal 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.
- Parameters:
divisor - value by which this BigDecimal is to be divided.
- Returns:
this / divisor
- Throws:
ArithmeticException - if the exact quotient does not have a
terminating decimal expansion
- Since:
- 1.5
-
divide
public BigDecimal divide(BigDecimal divisor,
MathContext mc)
Returns a BigDecimal whose value is (this /
divisor), with rounding according to the context settings.
- Parameters:
divisor - value by which this BigDecimal is to be divided.
mc - the context to use.
- Returns:
this / divisor, rounded as necessary.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY or
mc.precision == 0 and the quotient has a
non-terminating decimal expansion.
- Since:
- 1.5
-
divideToIntegralValue
public BigDecimal divideToIntegralValue(BigDecimal divisor)
Returns a BigDecimal whose value is the integer part
of the quotient (this / divisor) rounded down. The
preferred scale of the result is (this.scale() -
divisor.scale()).
- Parameters:
divisor - value by which this BigDecimal is to be divided.
- Returns:
- The integer part of
this / divisor.
- Throws:
ArithmeticException - if divisor==0
- Since:
- 1.5
-
divideToIntegralValue
public BigDecimal divideToIntegralValue(BigDecimal divisor,
MathContext mc)
Returns a BigDecimal whose value is the integer part
of (this / divisor). Since the integer part of the
exact quotient does not depend on the rounding mode, the
rounding mode does not affect the values returned by this
method. The preferred scale of the result is
(this.scale() - divisor.scale()). An
ArithmeticException is thrown if the integer part of
the exact quotient needs more than mc.precision
digits.
- Parameters:
divisor - value by which this BigDecimal is to be divided.
mc - the context to use.
- Returns:
- The integer part of
this / divisor.
- Throws:
ArithmeticException - if divisor==0
ArithmeticException - if mc.precision > 0 and the result
requires a precision of more than mc.precision digits.
- Since:
- 1.5
-
abs
public BigDecimal abs(MathContext mc)
Returns a BigDecimal whose value is the absolute value
of this BigDecimal, with rounding according to the
context settings.
- Parameters:
mc - the context to use.
- Returns:
abs(this), rounded as necessary.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
negate
public BigDecimal negate()
Returns a BigDecimal whose value is (-this),
and whose scale is this.scale().
- Returns:
-this.
-
negate
public BigDecimal negate(MathContext mc)
Returns a BigDecimal whose value is (-this),
with rounding according to the context settings.
- Parameters:
mc - the context to use.
- Returns:
-this, rounded as necessary.
- Throws:
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY.
- Since:
- 1.5
-
plus
public BigDecimal plus()
Returns a BigDecimal whose value is (+this), and whose
scale is this.scale().
This method, which simply returns this BigDecimal
is included for symmetry with the unary minus method negate().
- Returns:
this.
- Since:
- 1.5
- See Also:
negate()
-
scale
public int scale()
Returns the scale of this 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 -3 means the unscaled
value is multiplied by 1000.
- Returns:
- the scale of this
BigDecimal.
-
precision
public int precision()
Returns the precision of this BigDecimal. (The
precision is the number of digits in the unscaled value.)
The precision of a zero value is 1.
- Returns:
- the precision of this
BigDecimal.
- Since:
- 1.5
-
movePointLeft
public BigDecimal movePointLeft(int n)
Returns a BigDecimal which is equivalent to this one
with the decimal point moved n places to the left. If
n is non-negative, the call merely adds n to
the scale. If n is negative, the call is equivalent
to movePointRight(-n). The BigDecimal
returned by this call has value (this ×
10-n) and scale max(this.scale()+n,
0).
- Parameters:
n - number of places to move the decimal point to the left.
- Returns:
- a
BigDecimal which is equivalent to this one with the
decimal point moved n places to the left.
- Throws:
ArithmeticException - if scale overflows.
-
movePointRight
public BigDecimal movePointRight(int n)
Returns a BigDecimal which is equivalent to this one
with the decimal point moved n places to the right.
If n is non-negative, the call merely subtracts
n from the scale. If n is negative, the call
is equivalent to movePointLeft(-n). The
BigDecimal returned by this call has value (this
× 10n) and scale max(this.scale()-n,
0).
- Parameters:
n - number of places to move the decimal point to the right.
- Returns:
- a
BigDecimal which is equivalent to this one
with the decimal point moved n places to the right.
- Throws:
ArithmeticException - if scale overflows.
-
stripTrailingZeros
public BigDecimal stripTrailingZeros()
Returns a BigDecimal which is numerically equal to
this one but with any trailing zeros removed from the
representation. For example, stripping the trailing zeros from
the BigDecimal value 600.0, which has
[BigInteger, scale] components equals to
[6000, 1], yields 6E2 with [BigInteger,
scale] components equals to [6, -2]. If
this BigDecimal is numerically equal to zero, then
BigDecimal.ZERO is returned.
- Returns:
- a numerically equal
BigDecimal with any
trailing zeros removed.
- Since:
- 1.5
-
compareTo
public int compareTo(BigDecimal val)
Compares this BigDecimal with the specified
BigDecimal. Two BigDecimal objects that are
equal in value but have a different scale (like 2.0 and 2.00)
are considered equal by this method. This method is provided
in preference to individual methods for each of the six boolean
comparison operators (<, ==,
>, >=, !=, <=). The
suggested idiom for performing these comparisons is:
(x.compareTo(y) <op> 0), where
<op> is one of the six comparison operators.
- Specified by:
compareTo in interface Comparable<BigDecimal>
- Parameters:
val - BigDecimal to which this BigDecimal is
to be compared.
- Returns:
- -1, 0, or 1 as this
BigDecimal is numerically
less than, equal to, or greater than val.
-
toString
public String toString()
Returns the string representation of this BigDecimal,
using scientific notation if an exponent is needed.
A standard canonical string form of the BigDecimal
is created as though by the following steps: first, the
absolute value of the unscaled value of the BigDecimal
is converted to a string in base ten using the characters
'0' through '9' with no leading zeros (except
if its value is zero, in which case a single '0'
character is used).
Next, an adjusted exponent is calculated; this is the
negated scale, plus the number of characters in the converted
unscaled value, less one. That is,
-scale+(ulength-1), where ulength is the
length of the absolute value of the unscaled value in decimal
digits (its precision).
If the scale is greater than or equal to zero and the
adjusted exponent is greater than or equal to -6, the
number will be converted to a character form without using
exponential notation. In this case, if the scale is zero then
no decimal point is added and if the scale is positive a
decimal point will be inserted with the scale specifying the
number of characters to the right of the decimal point.
'0' characters are added to the left of the converted
unscaled value as necessary. If no character precedes the
decimal point after this insertion then a conventional
'0' character is prefixed.
Otherwise (that is, if the scale is negative, or the
adjusted exponent is less than -6), the number will be
converted to a character form using exponential notation. In
this case, if the converted BigInteger has more than
one digit a decimal point is inserted after the first digit.
An exponent in character form is then suffixed to the converted
unscaled value (perhaps with inserted decimal point); this
comprises the letter 'E' followed immediately by the
adjusted exponent converted to a character form. The latter is
in base ten, using the characters '0' through
'9' with no leading zeros, and is always prefixed by a
sign character '-' ('\u002D') if the
adjusted exponent is negative, '+'
('\u002B') otherwise).
Finally, the entire string is prefixed by a minus sign
character '-' ('\u002D') if the unscaled
value is less than zero. No sign character is prefixed if the
unscaled value is zero or positive.
Examples:
For each representation [unscaled value, scale]
on the left, the resulting string is shown on the right.
[123,0] "123"
[-123,0] "-123"
[123,-1] "1.23E+3"
[123,-3] "1.23E+5"
[123,1] "12.3"
[123,5] "0.00123"
[123,10] "1.23E-8"
[-123,12] "-1.23E-10"
Notes:
- There is a one-to-one mapping between the distinguishable
BigDecimal values and the result of this conversion.
That is, every distinguishable BigDecimal value
(unscaled value and scale) has a unique string representation
as a result of using toString. If that string
representation is converted back to a BigDecimal using
the BigDecimal(String) constructor, then the original
value will be recovered.
- The string produced for a given number is always the same;
it is not affected by locale. This means that it can be used
as a canonical string representation for exchanging decimal
data, or as a key for a Hashtable, etc. Locale-sensitive
number formatting and parsing is handled by the
NumberFormat class and its subclasses.
- The
toEngineeringString() method may be used for
presenting numbers with exponents in engineering notation, and the
setScale method may be used for
rounding a BigDecimal so it has a known number of digits after
the decimal point.
- The digit-to-character mapping provided by
Character.forDigit is used.
- Overrides:
toString in class Object
- Returns:
- string representation of this
BigDecimal.
- See Also:
Character.forDigit(int, int),
BigDecimal(java.lang.String)
-
toBigIntegerExact
public BigInteger toBigIntegerExact()
Converts this BigDecimal to a BigInteger,
checking for lost information. An exception is thrown if this
BigDecimal has a nonzero fractional part.
- Returns:
- this
BigDecimal converted to a BigInteger.
- Throws:
ArithmeticException - if this has a nonzero
fractional part.
- Since:
- 1.5
-
longValue
public long longValue()
Converts this BigDecimal to a long.
This conversion is analogous to the
narrowing primitive conversion from double to
short as defined in section 5.1.3 of
The Java™ Language Specification:
any fractional part of this
BigDecimal will be discarded, and if the resulting
"BigInteger" is too big to fit in a
long, only the low-order 64 bits are returned.
Note that this conversion can lose information about the
overall magnitude and precision of this BigDecimal value as well
as return a result with the opposite sign.
- Specified by:
longValue in class Number
- Returns:
- this
BigDecimal converted to a long.
-
longValueExact
public long longValueExact()
Converts this BigDecimal to a long, checking
for lost information. If this BigDecimal has a
nonzero fractional part or is out of the possible range for a
long result then an ArithmeticException is
thrown.
- Returns:
- this
BigDecimal converted to a long.
- Throws:
ArithmeticException - if this has a nonzero
fractional part, or will not fit in a long.
- Since:
- 1.5
-
intValue
public int intValue()
Converts this BigDecimal to an int.
This conversion is analogous to the
narrowing primitive conversion from double to
short as defined in section 5.1.3 of
The Java™ Language Specification:
any fractional part of this
BigDecimal will be discarded, and if the resulting
"BigInteger" is too big to fit in an
int, only the low-order 32 bits are returned.
Note that this conversion can lose information about the
overall magnitude and precision of this BigDecimal
value as well as return a result with the opposite sign.
- Specified by:
intValue in class Number
- Returns:
- this
BigDecimal converted to an int.
-
intValueExact
public int intValueExact()
Converts this BigDecimal to an int, checking
for lost information. If this BigDecimal has a
nonzero fractional part or is out of the possible range for an
int result then an ArithmeticException is
thrown.
- Returns:
- this
BigDecimal converted to an int.
- Throws:
ArithmeticException - if this has a nonzero
fractional part, or will not fit in an int.
- Since:
- 1.5
-
shortValueExact
public short shortValueExact()
Converts this BigDecimal to a short, checking
for lost information. If this BigDecimal has a
nonzero fractional part or is out of the possible range for a
short result then an ArithmeticException is
thrown.
- Returns:
- this
BigDecimal converted to a short.
- Throws:
ArithmeticException - if this has a nonzero
fractional part, or will not fit in a short.
- Since:
- 1.5
-
byteValueExact
public byte byteValueExact()
Converts this BigDecimal to a byte, checking
for lost information. If this BigDecimal has a
nonzero fractional part or is out of the possible range for a
byte result then an ArithmeticException is
thrown.
- Returns:
- this
BigDecimal converted to a byte.
- Throws:
ArithmeticException - if this has a nonzero
fractional part, or will not fit in a byte.
- Since:
- 1.5
-
ulp
public BigDecimal ulp()
Returns the size of an ulp, a unit in the last place, of this
BigDecimal. An ulp of a nonzero BigDecimal
value is the positive distance between this value and the
BigDecimal value next larger in magnitude with the
same number of digits. An ulp of a zero value is numerically
equal to 1 with the scale of this. The result is
stored with the same scale as this so the result
for zero and nonzero values is equal to [1,
this.scale()].
- Returns:
- the size of an ulp of
this
- Since:
- 1.5
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|