BigDecimal precision() Method in Java
Last Updated :
04 Dec, 2018
The java.math.BigDecimal.precision() method returns the precision of this BigDecimal. The precision refers to the number of digits in the unscaled value.
Syntax:
public int precision()
Parameters: This method does not accept any parameters.
Return Value: This method returns an integer which denotes the precision of this BigDecimal object.
Examples:
Input : 198.176
Output : 6
Input : 721111.111
Output : 9
Below programs illustrate the java.math.BigDecimal.precision() function in Java:
Program 1:
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
BigDecimal b1, b2;
b1 = new BigDecimal( "198.176" );
b2 = new BigDecimal( "721111.111" );
System.out.println( "The precision of " + b1 + " is " + b1.precision());
System.out.println( "The precision of " + b2 + " is " + b2.precision());
}
}
|
Output:
The precision of 198.176 is 6
The precision of 721111.111 is 9
Program 2:
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
BigDecimal num;
num = new BigDecimal( "0.1" )
.add( new BigDecimal( "0.1" ))
.add( new BigDecimal( "0.1" ));
System.out.println( "The precision of " + num + " is "
+ num.precision());
}
}
|
Output:
The precision of 0.3 is 1
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#precision()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...