Home »
Java »
Java Reference »
Java BigDecimal Class
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