Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

java.math.MathContext Class in Java

  • Last Updated : 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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • 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

equals(Object x)This method compares this MathContext with the specified Object for equality.
getPrecision()This method returns the precision setting.
getRoundingMode()This method returns the roundingMode setting.
hashCode()This method returns the hash code for this MathContext.
toString()This method returns the string representation of this MathContext.

Example:

Java




// Java Program to illustrate java.math.Context class
  
// Importing all classes from
// java.math package
import java.math.*;
  
// Main class
class 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)));
    }
}
Output
246.8
2.5E+2
2.5E+2
2E+2
3E+2



My Personal Notes arrow_drop_up
Recommended Articles
Page :