The
java.math.BigDecimal.setScale() is used to set the scale of BigDecimal. This method performs an operation upon the current BigDecimal by which this method is called.
There are three overloads of setScale() method available in Java which is listed below:
- setScale(int newScale)
- setScale(int newScale, int roundingMode)
- setScale(int newScale, RoundingMode roundingMode)
Note: The
setScale(int newScale, int roundingMode) is
deprecated since Java 9.
setScale(int newScale)
This call is typically used to increase the scale when 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 user knows that the BigDecimal has sufficiently many zeros at the end of its fractional part to allow for the rescaling without changing its value.
Syntax:
public BigDecimal setScale(int newScale)
Parameters: This method accepts a parameter
newScale which is used to set the scale of this BigDecimal.
Return value: This method returns a BigDecimal whose scale is of the specified value.
Exception: If the specified scaling operation would require rounding then
Arithmetic Exception is thrown.
Note: Since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, setScale returns an object with the proper scale. The returned object may or may not be newly allocated.
Below programs is used to illustrate the setScale() method of BigDecimal.
Output:
31452678569.2500
setScale(int newScale, int roundingMode)
This method is used to calculate 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 then unscaled value must be divided (rather than multiplied). The specified rounding mode is applied to the division.
Syntax:
public BigDecimal setScale(int newScale, int roundingMode)
Parameters: This method accepts two parameter
newScale which is used to set the scale of this BigDecimal and
roundingMode to set the rounding value of result.
Return value: This method returns a BigDecimal whose scale is of the specified value.
Exception: The method throws
Arithmetic Exception if roundingMode is UNNECESSARY and the specified scaling operation would require rounding. This method also throws
IllegalArgumentException if roundingMode does not represent a valid rounding mode.
Below programs is used to illustrate the setScale() method of BigDecimal.
Example 1:
Output:
3.145267856E+10
Example 2: Program showing Exceptions thrown by this method.
Output:
java.lang.ArithmeticException: Rounding necessary
java.lang.IllegalArgumentException: Invalid rounding mode
setScale(int newScale, RoundingMode roundingMode)
This method is used to calculate 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 then unscaled value must be divided (rather than multiplied). The specified rounding mode is applied to the division.
Syntax:
public BigDecimal setScale(int newScale, RoundingMode roundingMode)
Parameters: This method accepts two parameter
newScale which is used to set the scale of this BigDecimal and
roundingMode of type
RoundingMode that tells which rounding mode to apply.
Return value: This method returns a BigDecimal whose scale is of the specified value.
Exception: The method throws
Arithmetic Exception if roundingMode is UNNECESSARY and the specified scaling operation would require rounding.
Below programs is used to illustrate the setScale() method of BigDecimal.
Example 1:
Output:
31452678569.3
Example 2: Program showing Exceptions thrown by this method.