Java BigDecimal setScale() Method with Example

BigDecimal Class setScale() method: Here, we are going to learn about the setScale() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 07, 2020

BigDecimal Class setScale() method

Syntax:

    public BigDecimal setScale(int sc);
    public BigDecimal setScale(int sc, int r_status);
    public BigDecimal setScale(int sc, RoundingMode rm);
  • setScale() method is available in java.math package.
  • setScale(int sc) method is used to set the new scale with the given scale of this BigDecimal object.
  • setScale(int sc, int r_status) method is used to set the new scale with the given scale value, and its non-scale value is calculated by either multiply or divide this BigDecimal non-scaled value by the power of 10.
  • setScale(int sc, RoundingMode rm) method is used to set the new scale with the given scale value, and its non-scale value is calculated by either multiply or divide this BigDecimal non-scaled value by the power of 10.
  • These methods may throw an exception at the time of setting a scale.
    • ArithmeticException: This exception may throw when the given parameter holds constants ROUND_UNNECESSARY and the given scaling operation need rounding.
    • IllegalArgumentException: This exception may throw when the given parameter r_status is invalid.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, setScale(int sc),
    • int sc – represents the new scale value of this BigDecimal.
  • In the first case, setScale(int sc, int r_status),
    • int sc – Similar as defined in the first case.
    • int r_status – represents the rounding status.
  • In the first case, setScale(int sc, RoundingMode rm),
    • int sc – Similar as defined in the first case.
    • RoundingMode rm – represents the mode of rounding to implement and it is of "RoundingMode" type.

Return value:

In all the cases, the return type of the method is BigDecimal, it returns the BigDecimal object and its scale value is set by the given parameter and its unscale value is calculated by either divide or multiply this BigDecimal unscaled value by the power of 10.

Example:

// Java program to demonstrate the example 
// of setScale() method of BigDecimal

import java.math.*;

public class SetScaleOfBD {
    public static void main(String args[]) {
        // Initialize a variables val
        String val = "10.3045";

        // Initialize a BigDecimal objects 
        BigDecimal b_dec = new BigDecimal(val);

        System.out.println("scale(): ");

        // Get old scale
        int old_scale = b_dec.scale();
        System.out.println("b_dec: " + b_dec);
        System.out.println("b_dec.scale(): " + old_scale);

        System.out.println(" ");

        System.out.println("setScale(int): ");

        // sets the new scale of this
        // BigDecimal b_dec and scale is the
        // number of digits represented after
        // decimal
        BigDecimal bd = b_dec.setScale(5);

        // Get new set scale
        int new_scale = bd.scale();
        System.out.println("b_dec: " + bd);
        System.out.println("b_dec.setScale(5): " + new_scale);

        System.out.println(" ");

        System.out.println("setScale(int,int): ");

        // sets the new scale of this
        // BigDecimal b_dec with the given rounding
        // mode "2" 
        bd = b_dec.setScale(3, 2);

        // Get new set scale
        new_scale = bd.scale();
        System.out.println("b_dec: " + bd);
        System.out.println("bd.setScale(3,2): " + new_scale);

        System.out.println(" ");

        System.out.println("setScale(int,RoundingMode): ");

        // sets the new scale of this
        // BigDecimal b_dec with the given RoundingMode
        // RoundingMode.CEILING
        bd = b_dec.setScale(3, RoundingMode.CEILING);

        // Get new set scale
        new_scale = bd.scale();
        System.out.println("b_dec: " + bd);
        System.out.println("b_dec.setScale(3,RoundingMode.CEILING): " + new_scale);
    }
}

Output

scale(): 
b_dec: 10.3045
b_dec.scale(): 4
 
setScale(int): 
b_dec: 10.30450
b_dec.setScale(5): 5
 
setScale(int,int): 
b_dec: 10.305
bd.setScale(3,2): 3
 
setScale(int,RoundingMode): 
b_dec: 10.305
b_dec.setScale(3,RoundingMode.CEILING): 3



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.