ADVERTISEMENT
ADVERTISEMENT

Java BigDecimal scale() Method with Example

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

BigDecimal Class scale() method

  • scale() method is available in java.math package.
  • scale() method is used to get the scale of this BigDecimal object. As we will define the scale when this BigInteger is 0, +ve,-ve.
    • Case 1: The scale is the number of digits represented to the right side of decimal when this BigDecimal is 0 or +ve.
    • Case 2: The scale is the unscaled value of the [number * 10 pow (-[-( this BigDecimal)]).
  • scale() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • scale() method does not throw an exception at the time of scaling.

Syntax:

    public int scale();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it returns this BigDecimal scale.

Example:

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

import java.math.*;

public class ScaleOfBD {
    public static void main(String args[]) {
        // Initialize two variables and
        // both are of "String" type
        String val1 = "100.24";
        String val2 = "-2.1456";

        // Initialize two BigDecimal objects and
        // one MathContext
        BigDecimal b_dec1 = new BigDecimal(val1);
        BigDecimal b_dec2 = new BigDecimal(val2);


        // the scale of this BigDecimal b_dec1
        // i.e. scale is the number of digits 
        // represented after decimal point here
        // val1 = 100.24 (i.e. 24 two digits after
        // decimal)
        int scale = b_dec1.scale();
        System.out.println("b_dec1.scale(): " + scale);

        // the scale of this BigDecimal b_dec2
        // i.e. scale is the number of digits 
        // represented after decimal point here
        // val2 = -2.1456 (i.e. 1456 four digits after
        // decimal)
        scale = b_dec2.scale();
        System.out.println("b_dec2.scale(): " + scale);
    }
}

Output

b_dec1.scale(): 2
b_dec2.scale(): 4
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.