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. If the
scale is reduced by the operation, the unscaled value must be
divided (rather than multiplied), and the value may be changed;
in this case, the specified rounding mode is applied to the
division.
newScale | scale of the BigDecimal value to be returned. | |
roundingMode | The rounding mode to apply. |
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.
ArithmeticException | if roundingMode==ROUND_UNNECESSARY
and the specified scaling operation would require
rounding. | |
IllegalArgumentException | if roundingMode does not
represent a valid rounding mode. |
ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_UNNECESSARY
@apiNote Since BigDecimal objects are immutable, calls of
this method do not result in the original object being
modified, contrary to the usual convention of having methods
named setX
mutate field X
.
Instead, setScale
returns an object with the proper
scale; the returned object may or may not be newly allocated.
Diagram: BigInteger, BigDecimal