BigDecimal.setScale

BigDecimal.setScale

Class Overview | Class Members | This Package | All Packages

Syntax 1
public BigDecimal setScale( int scale, int roundingMode ) throws ArithmeticException, IllegalArgumentException
Description
Returns a BigDecimal whose scale is the specified value, and whose integer value is determined by multiplying or dividing this BigDecimal's integer value by the appropriate power of ten to maintain the overall value. If the scale is reduced by the operation, the integer value must be divided (rather than multiplied), and precision may be lost; in this case, the specified rounding mode is applied to the division. Throws an ArithmeticException if scale is negative, or the rounding mode is ROUND_UNNECESSARY and it is impossible to perform the specified scaling operation without loss of precision. Throws an IllegalArgumentException if roundingMode does not represent a valid rounding mode.



Syntax 2
public BigDecimal setScale( int scale ) throws ArithmeticException, IllegalArgumentException
Description
Returns a BigDecimal whose scale is the specified value, and whose value is exactly equal to this number's. Throws an ArithmeticException if this is not possible. This call is typically used to increase the scale, in which case it is guaranteed that there exists a BigDecimal of the specified scale and the correct value. The call can also be used to reduce the scale if the caller knows that the number has sufficiently many zeros at the end of its fractional part (i.e., factors of ten in its integer value) to allow for the rescaling without loss of precision. Note that this call returns the same result as the two argument version of setScale, but saves the caller the trouble of specifying a rounding mode in cases where it is irrelevant.