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()