Thursday, April 2, 2026
HomeLanguagesJavaMathContext getPrecision() Method in Java

MathContext getPrecision() Method in Java

The java.math.MathContext.getPrecision() is an in-built function in Java which returns the precision settings of the MathContext object. This value is always non-negative.

Syntax :

public int getPrecision()

Parameters: The method does not take any parameters.

Return Value: This method returns an int which is the value of the precision setting of the MathContext object.

Examples:

Input : m1 = new MathContext(7); 
Output : 7

Input : m1 = new MathContext(2, RoundingMode.HALF_UP);
Output : 2

Below programs illustrate the use of getPrecision() function :
Program 1 :




// Java program to demonstrate getPrecision() method
import java.math.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating a MathContext object m1
        MathContext m1;
  
        // Assign context settings to m1
        m1 = new MathContext(2, RoundingMode.HALF_UP);
  
        // Displaying the result
        System.out.println(m1.getPrecision());
    }
}


Output:

2

Program 2 :




// Java program to demonstrate getPrecision() method
import java.math.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating a MathContext object
        MathContext m1;
  
        // Assign context settings to m1
        m1 = new MathContext(50);
  
        // Displaying the result
        System.out.println(m1.getPrecision());
    }
}


Output:

50

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/MathContext.html#getPrecision()

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6885 POSTS0 COMMENTS
Nicole Veronica
12006 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12100 POSTS0 COMMENTS
Shaida Kate Naidoo
7015 POSTS0 COMMENTS
Ted Musemwa
7259 POSTS0 COMMENTS
Thapelo Manthata
6971 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS