public final class

MathContext

extends Object
implements Serializable
java.lang.Object
   ↳ java.math.MathContext

Class Overview

Immutable objects describing settings such as rounding mode and digit precision for the numerical operations provided by class BigDecimal.

Summary

Constants
MathContext DECIMAL128 A MathContext which corresponds to the IEEE 754r quadruple decimal precision format: 34 digit precision and HALF_EVEN rounding.
MathContext DECIMAL32 A MathContext which corresponds to the IEEE 754r single decimal precision format: 7 digit precision and HALF_EVEN rounding.
MathContext DECIMAL64 A MathContext which corresponds to the IEEE 754r double decimal precision format: 16 digit precision and HALF_EVEN rounding.
MathContext UNLIMITED A MathContext for unlimited precision with HALF_UP rounding.
Public Constructors
MathContext(int precision)
Constructs a new MathContext with the specified precision and with the rounding mode HALF_UP.
MathContext(int precision, RoundingMode roundingMode)
Constructs a new MathContext with the specified precision and with the specified rounding mode.
MathContext(String val)
Constructs a new MathContext from a string.
Public Methods
boolean equals(Object x)
Returns true if x is a MathContext with the same precision setting and the same rounding mode as this MathContext instance.
int getPrecision()
Returns the precision.
RoundingMode getRoundingMode()
Returns the rounding mode.
int hashCode()
Returns the hash code for this MathContext instance.
String toString()
Returns the string representation for this MathContext instance.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final MathContext DECIMAL128

A MathContext which corresponds to the IEEE 754r quadruple decimal precision format: 34 digit precision and HALF_EVEN rounding.

public static final MathContext DECIMAL32

A MathContext which corresponds to the IEEE 754r single decimal precision format: 7 digit precision and HALF_EVEN rounding.

public static final MathContext DECIMAL64

A MathContext which corresponds to the IEEE 754r double decimal precision format: 16 digit precision and HALF_EVEN rounding.

public static final MathContext UNLIMITED

A MathContext for unlimited precision with HALF_UP rounding.

Public Constructors

public MathContext (int precision)

Constructs a new MathContext with the specified precision and with the rounding mode HALF_UP. If the precision passed is zero, then this implies that the computations have to be performed exact, the rounding mode in this case is irrelevant.

Parameters
precision the precision for the new MathContext.
Throws
IllegalArgumentException if precision < 0.

public MathContext (int precision, RoundingMode roundingMode)

Constructs a new MathContext with the specified precision and with the specified rounding mode. If the precision passed is zero, then this implies that the computations have to be performed exact, the rounding mode in this case is irrelevant.

Parameters
precision the precision for the new MathContext.
roundingMode the rounding mode for the new MathContext.
Throws
IllegalArgumentException if precision < 0.
NullPointerException if roundingMode is null.

public MathContext (String val)

Constructs a new MathContext from a string. The string has to specify the precision and the rounding mode to be used and has to follow the following syntax: "precision=<precision> roundingMode=<roundingMode>" This is the same form as the one returned by the toString() method.

Parameters
val a string describing the precision and rounding mode for the new MathContext.
Throws
IllegalArgumentException if the string is not in the correct format or if the precision specified is < 0.

Public Methods

public boolean equals (Object x)

Returns true if x is a MathContext with the same precision setting and the same rounding mode as this MathContext instance.

Parameters
x object to be compared.
Returns
  • true if this MathContext instance is equal to the x argument; false otherwise.

public int getPrecision ()

Returns the precision. The precision is the number of digits used for an operation. Results are rounded to this precision. The precision is guaranteed to be non negative. If the precision is zero, then the computations have to be performed exact, results are not rounded in this case.

Returns
  • the precision.

public RoundingMode getRoundingMode ()

Returns the rounding mode. The rounding mode is the strategy to be used to round results.

The rounding mode is one of UP, DOWN, CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_EVEN, or UNNECESSARY.

Returns
  • the rounding mode.

public int hashCode ()

Returns the hash code for this MathContext instance.

Returns
  • the hash code for this MathContext.

public String toString ()

Returns the string representation for this MathContext instance. The string has the form "precision=<precision> roundingMode=<roundingMode>" where <precision> is an integer describing the number of digits used for operations and <roundingMode> is the string representation of the rounding mode.

Returns
  • a string representation for this MathContext instance