Home »
Java »
Java Reference »
Java MathContext Class
Java MathContext Class | getPrecision() Method with Example
MathContext Class getPrecision() method: Here, we are going to learn about the getPrecision() method of MathContext Class with its syntax and example.
Submitted by Preeti Jain, on May 12, 2020
MathContext Class getPrecision() method
- getPrecision() method is available in java.math package.
- getPrecision() method is used to get the precision settings of this MathContext object when it exists.
- getPrecision() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- getPrecision() method does not throw an exception at the time of returning precision.
Syntax:
public int getPrecision();
Parameter(s):
Return value:
The return type of this method is int, it returns the precision value of this MathContext object.
Example:
// Java program to demonstrate the example
// of int getPrecision() method of MathContext
import java.math.*;
public class GetPrecisionOfMC {
public static void main(String args[]) {
// Initialize three MathContext objects
MathContext ma_co1 = new MathContext(3, RoundingMode.HALF_UP);
MathContext ma_co2 = new MathContext(4);
MathContext ma_co3 = new MathContext(6, RoundingMode.FLOOR);
// returns the precision of this MathContext
// ma_co1 i.e. precision is the first
// parameter set in MathContext Constructor
// i.e. 3 in ma_co1
int precision = ma_co1.getPrecision();
System.out.println("ma_co1.getPrecision(): " + precision);
// returns the precision of this MathContext
// ma_co2 i.e. precision is the first
// parameter set in MathContext Constructor
// i.e. 4 in ma_co2
precision = ma_co2.getPrecision();
System.out.println("ma_co2.getPrecision(): " + precision);
// returns the precision of this MathContext
// ma_co3 i.e. precision is the first
// parameter set in MathContext Constructor
// i.e. 6 in ma_co3
precision = ma_co3.getPrecision();
System.out.println("ma_co3.getPrecision(): " + precision);
}
}
Output
ma_co1.getPrecision(): 3
ma_co2.getPrecision(): 4
ma_co3.getPrecision(): 6