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) { // create 2 BigDecimal Objects BigDecimal b1, b2; // Assigning values to b1, b2 b1 = new BigDecimal("198.176"); b2 = new BigDecimal("721111.111"); // Display their respective precision System.out.println("The precision of " + b1 + " is " + b1.precision()); System.out.println("The precision of " + b2 + " is " + b2.precision()); } } |
The precision of 198.176 is 6 The precision of 721111.111 is 9
Program 2:
// Java program to illustrate // precision() Function import java.math.*; import java.io.*; class GFG { public static void main(String[] args) { // Creating a BigDecimal Object BigDecimal num; // Assigning value 0.1 + 0.1 + 0.1 to num num = new BigDecimal("0.1") .add(new BigDecimal("0.1")) .add(new BigDecimal("0.1")); // Display the BigDecimal value and its precision System.out.println("The precision of " + num + " is " + num.precision()); } } |
The precision of 0.3 is 1
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#precision()
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to set Precision for Double values in Java?
- BigDecimal min() Method in Java
- BigDecimal max() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal valueOf() Method in Java
- BigDecimal byteValueExact() Method in Java
- BigDecimal doubleValue() Method in Java
- BigDecimal ulp() Method in Java
- BigDecimal toBigIntegerExact() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal stripTrailingZeros() Method in Java
- BigDecimal equals() Method in Java
- BigDecimal shortValueExact() Method in Java
- BigDecimal signum() Method in Java
- BigDecimal longValue() Method in Java
- BigDecimal hashCode() Method in Java
- BigDecimal intValue() Method in Java
- BigDecimal longValueExact() Method in Java
- BigDecimal intvalueExact() Method in Java
- BigDecimal round() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

