Java Reference
In-Depth Information
bigNum = new BigDecimal("1.23");
This constructor translates the string operand into a BigDecimal value.
The string consists of an optional positive or negative sign, or no sign for
positive values, followed by a sequence of zero or more digits that form
the integer part of the number, optionally followed by a decimal point, op-
tionally followed by a fractional part, optionally followed by an exponent.
The string must contain at least one digit in either the integer or the frac-
tional part. The portion of the number formed by the sign, the integer and
the fraction is referred to as the significand.
The exponent can consist of the character e or E, followed by one or
more decimal digits. The value of the exponent must lie between Inte-
ger.MIN_VALUE and Integer.MAX_VALUE, inclusive.
The scale of the BigDecimal value produced by the constructor will be
the number of digits in the fraction, or zero if the string contains no deci-
mal point. If there is an exponent, the scale is adjusted by subtracting the
exponent. If the resulting value is negative, the scale of the BigDecimal is
zero. In any case the resulting BigDecimal is
significand * 10 exponent
For floats and doubles other that NAN, +INFINITY and -INFINITY, the
string constructor is compatible with the values returned by
Float.toString() and Double.toString(). Using the toString() methods is
the recommended way to convert a float or double into a BigDecimal, as
it avoids the unpredictability of the BigDecimal(double) constructor.
BigDecimal scale operations
Two types of operations are provided for manipulating the scale of
BigDecimal numbers: those that relate to setting and changing the scale
and rounding controls, and those that move the decimal point.
SetScale() method
ThesetScale()methodisprovidedwithtwodifferentsignatures.Onetakes
asparametersascaleandaroundingmode,andtheotheronejustthescale.
Recallthatthescaleisthenumberofrepresentabledigitstotherightofthe
decimal point. The setScale() methods are as follows:
public BigDecimal setScale(int scale, int roundingMode)
ReturnsaBigDecimalwhosescaleandroundingmodearethespecifiedval-
ues. The method returns a BigDecimal whose scale is the specified value,
and whose unscaled value is determined by multiplying or dividing this
Search WWH ::




Custom Search