Friday, October 17, 2025
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

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS