Java.math.BigDecimal.setScale() Method

Description

The java.math.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.

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.

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.

Declaration

Following is the declaration for java.math.BigDecimal.setScale() method

public BigDecimal setScale(int newScale, RoundingMode roundingMode)

Parameters

newScale - scale of the BigDecimal value to be returned

roundingMode - the rounding mode to apply

Return Value

This 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.