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:
- 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)));
}
}
Output246.8
2.5E+2
2.5E+2
2E+2
3E+2
Similar Reads
Java.lang.Number Class in Java Most of the time, while working with numbers in java, we use primitive data types. But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. There are mainly six sub-classes under Number class.These sub-classes define some useful method
9 min read
java.math class and its methods | Set 1 Math class provides mathematical functions to perform basic numeric operations such as exponential, logarithm, square root, and trigonometric functions. cosh, sin, tan, abs, bitLength, multiply and many more. Implementations of the functions of Math class do not return bit-for-bit same results. Henc
4 min read
java.math class and its methods | Set 3 java.math class and its methods | Set 1 java.math class and its methods | Set 2 ceil() : java.math.ceil(double a) method returns the smallest possible value which is either greater or equal to the argument passed. The returned value is a mathematical integer.Special Case : Result is same, if the ret
5 min read
java.lang.reflect.Proxy Class in Java A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria
4 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
Java.Lang.Float class in Java Float class is a wrapper class for the primitive type float which contains several methods to effectively deal with a float value like converting it to a string representation, and vice-versa. An object of the Float class can hold a single float value. There are mainly two constructors to initialize
6 min read
MathContext equals() Method in Java The equals() method in the Java MathContext class is a part of the java.math package. This method is used to compare one MathContext object with another and checks whether both objects have the same precision and rounding mode settings.This method is very useful when we want to verify if two MathCon
2 min read
Static class in Java Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
3 min read
Java.Lang.Double Class in Java Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the pr
4 min read
java.lang.reflect.Field Class in Java The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t
5 min read