Home

BigDecimal

BigDecimal is a class in the Java standard library, java.math.BigDecimal, used for representing and performing arithmetic on decimal numbers with arbitrary precision. It provides exact decimal representation and arithmetic, making it suitable for financial calculations where floating point errors are unacceptable.

A BigDecimal stores its value as an unscaled BigInteger and a scale. The numeric value equals unscaledValue

Operations include add, subtract, multiply, and divide. Division methods may require a scale and rounding mode

Construction: the preferred way is to create a BigDecimal from a ASCII decimal string to avoid binary

Performance and scope: BigDecimal provides exact arithmetic at the cost of speed and memory compared to primitive

×
10^-scale.
The
instance
is
immutable;
arithmetic
operations
return
new
BigDecimal
instances
rather
than
modifying
the
operands.
or
a
MathContext
to
specify
precision
and
rounding
behavior.
RoundingMode
and
MathContext
control
how
results
are
rounded.
The
class
also
supports
setScale
to
adjust
the
number
of
decimal
places.
Other
utility
methods
include
compareTo,
equals,
toString,
precision,
scale,
and
unscaledValue.
floating
point
issues,
e.g.,
new
BigDecimal("123.45").
Constructors
from
long,
int,
or
double
exist
but
can
introduce
rounding.
BigDecimal.valueOf(double)
is
often
a
safe
alternative
because
it
uses
a
canonical
string
representation
of
the
double.
BigInteger-based
constructors
are
used
for
advanced
needs.
types.
It
is
not
intended
for
scientific
computing
where
wide
ranges
and
performance
are
critical;
it
does
not
provide
native
transcendental
functions.
It
is
Serializable
and
part
of
the
java.math
package.