java.math.MathContext Class in Java

22 Feb, 2021
The java.math.MathContext class provides immutable objects that encapsulate context settings and define those numerical operator rules, such as those that the BigDecimal class implements.
The base-independent configurations are as follows:
Precision: The number of digits to be used for an operation; this accuracy is rounded to results.
RoundingMode: a RoundingMode object that determines the rounding algorithm to be used.
The number of significant digits is specified by the precision. The default mode in rounding off is HALF_UP mode which will be introduced in later part.
Illustration:
Suppose a random number say be it 123 is selected and the task now is to round off for 2 significant digits, you’re going to get 120. It might be more apparent if you think in terms of scientific notation. As in scientific notations, 123 would be 1.23e2. If you only keep 2 significant digits, then you get 1.2e2, or 120. By reducing the number of significant digits, we reduce the precision with which we can specify a number.
The RoundingMode part specifies how we should handle the loss of precision. If you use 123 as the number and ask for 2 significant digits, you’ve reduced your accuracy to reuse the example. With a RoundingMode of HALF_UP (the default mode), 123 will become 120. With a RoundingMode of CEILING, you’ll get 130.
Syntax: Class declaration
public final class MathContext extends Object  implements Serializable
Constructor:
MathContext(int setPrecision): This constructor constructs a new MathContext with the specified precision and the HALF_UP rounding mode.
MathContext(int setPrecision, RoundingMode setRoundingMode): This constructor constructs a new MathContext with a specified precision and rounding mode.
MathContext(String val): This constructor constructs a new MathContext from a string.
Now, dwelling onto the methods present in this class, later on, to be used in the implementation part in the program for the same.
Method
Description
Example:
Java
// Java Program to illustrate java.math.Context class  // Importing all classes from// java.math packageimport java.math.*;  // Main classclass GFG {      // Main driver method    public static void main(String args[])    {          // Custom input number 'N' over which        // class operation are performed        // N = 246.8          // erforming the rounding of operations          // Rounding  off is carried out across        // 4 digits        // N = 246.8        // It has 4 digits only so        // the output is same as input          // Case 1        // Across all digits of the input N = 4        System.out.println(new BigDecimal(            "246.8",            new MathContext(4, RoundingMode.HALF_UP)));          // Case 2        // Across 'N/2' of the input 'N'        // Here, acrossings 2 digits as input N has 4 digits          // Rounding HALF_UP        System.out.println(new BigDecimal(            "246.8",            new MathContext(2, RoundingMode.HALF_UP)));          // Rounding HALF_DOWN        System.out.println(new BigDecimal(            "246.8",            new MathContext(2, RoundingMode.CEILING)));          // Case 3        // Across '1' digit of the input 'N'        // Here, acrossings 2 digits of 4 digits of input N          // Rounding HALF_UP        System.out.println(new BigDecimal(            "246.8",            new MathContext(1, RoundingMode.HALF_UP)));          // Rounding HALF_DOWN        System.out.println(new BigDecimal(            "246.8",            new MathContext(1, RoundingMode.CEILING)));    }}
246.8
2.5E+2
2.5E+2
2E+2
3E+2
Java-Classes
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.