BigDecimal setScale() method in Java with Examples

17 Jun, 2019
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.
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.
// Java program to demonstrate// setScale() method of BigDecimal  import java.math.BigDecimal;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store the result        BigDecimal res;          // For user input        // Use Scanner or BufferedReader          // Object of String created        // Holds the value        String input1            = "31452678569.25";          // Convert the string input to BigDecimal        BigDecimal a            = new BigDecimal(input1);          // Scale for BigDecimal        int newScale = 4;          // Using setScale() method        res = a.setScale(newScale);          // Display the result in BigDecimal        System.out.println(res);    }}
31452678569.2500

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:
// Java program to demonstrate// setScale() method of BigDecimal  import java.math.BigDecimal;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store the result        BigDecimal res;          // For user input        // Use Scanner or BufferedReader          // Object of String created        // Holds the value        String input1            = "31452678569.24";          // Convert the string input to BigDecimal        BigDecimal a            = new BigDecimal(input1);          // Scale for BigDecimal        int newScale = -1;        try {              // Using setScale() method            res = a.setScale(newScale, 1);              // Display the result in BigDecimal            System.out.println(res);        }        catch (Exception e) {              // Print Exception            System.out.println(e);        }    }}
3.145267856E+10

Example 2: Program showing Exceptions thrown by this method.
// Java program to demonstrate// setScale() method of BigDecimal  import java.math.BigDecimal;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store the result        BigDecimal res;          // For user input        // Use Scanner or BufferedReader          // Object of String created        // Holds the value        String input1            = "31452678569.24";          // Convert the string input to BigDecimal        BigDecimal a            = new BigDecimal(input1);          // Scale for BigDecimal        int newScale = -1;          try {              // Using setScale() method            res = a.setScale(newScale, 7);              // Display the result in BigDecimal            System.out.println(res);        }        catch (Exception e) {              // Print Exception            System.out.println(e);        }          try {              // Using setScale() method            res = a.setScale(newScale, 10);              // Display the result in BigDecimal            System.out.println(res);        }        catch (Exception e) {              // Print Exception            System.out.println(e);        }    }}
java.lang.ArithmeticException: Rounding necessary
java.lang.IllegalArgumentException: Invalid rounding mode

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:
// Java program to demonstrate// setScale() method of BigDecimal  import java.math.*;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store the result        BigDecimal res;          // For user input        // Use Scanner or BufferedReader          // Object of String created        // Holds the value        String input1            = "31452678569.24";          // Convert the string input to BigDecimal        BigDecimal a            = new BigDecimal(input1);          // Scale for BigDecimal        int newScale = 1;          try {              // Using setScale() method            // Using RoundingMode.CEILING            res = a.setScale(newScale, RoundingMode.CEILING);              // Display the result in BigDecimal            System.out.println(res);        }        catch (Exception e) {              // Print Exception            System.out.println(e);        }    }}
31452678569.3

Example 2: Program showing Exceptions thrown by this method.
// Java program to demonstrate// setScale() method of BigDecimal  import java.math.*;  public class GFG {    public static void main(String[] args)    {        // BigDecimal object to store the result        BigDecimal res;          // For user input        // Use Scanner or BufferedReader          // Object of String created        // Holds the value        String input1            = "31452678569.24";          // Convert the string input to BigDecimal        BigDecimal a            = new BigDecimal(input1);          // Scale for BigDecimal        int newScale = -1;          try {              // Using setScale() method            // Using RoundingMode.UNNECESSARY            res = a.setScale(newScale, RoundingMode.UNNECESSARY);              // Display the result in BigDecimal            System.out.println(res);        }        catch (Exception e) {              // Print Exception            System.out.println(e);        }    }}
java.lang.ArithmeticException: Rounding necessary

References: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#setScale(int)
Java-BigDecimal
Java-Functions
Java-math-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java