Package java.math
Class MathContext
java.lang.Object
java.math.MathContext
- All Implemented Interfaces:
Serializable
public final class MathContext extends Object implements Serializable
Immutable objects describing settings such as rounding mode and digit
precision for the numerical operations provided by class
BigDecimal.- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static MathContextDECIMAL128AMathContextwhich corresponds to the IEEE 754 quadruple decimal precision format: 34 digit precision andRoundingMode.HALF_EVENrounding.static MathContextDECIMAL32AMathContextwhich corresponds to the IEEE 754 single decimal precision format: 7 digit precision andRoundingMode.HALF_EVENrounding.static MathContextDECIMAL64AMathContextwhich corresponds to the IEEE 754 double decimal precision format: 16 digit precision andRoundingMode.HALF_EVENrounding.static MathContextUNLIMITEDAMathContextfor unlimited precision withRoundingMode.HALF_UProunding. -
Constructor Summary
Constructors Constructor Description MathContext(int precision)Constructs a newMathContextwith the specified precision and with the rounding modeHALF_UP.MathContext(int precision, RoundingMode roundingMode)Constructs a newMathContextwith the specified precision and with the specified rounding mode.MathContext(String s)Constructs a newMathContextfrom a string. -
Method Summary
Modifier and Type Method Description booleanequals(Object x)Returns true if x is aMathContextwith the same precision setting and the same rounding mode as thisMathContextinstance.intgetPrecision()Returns the precision.RoundingModegetRoundingMode()Returns the rounding mode.inthashCode()Returns the hash code for thisMathContextinstance.StringtoString()Returns the string representation for thisMathContextinstance.
-
Field Details
-
DECIMAL128
AMathContextwhich corresponds to the IEEE 754 quadruple decimal precision format: 34 digit precision andRoundingMode.HALF_EVENrounding. -
DECIMAL32
AMathContextwhich corresponds to the IEEE 754 single decimal precision format: 7 digit precision andRoundingMode.HALF_EVENrounding. -
DECIMAL64
AMathContextwhich corresponds to the IEEE 754 double decimal precision format: 16 digit precision andRoundingMode.HALF_EVENrounding. -
UNLIMITED
AMathContextfor unlimited precision withRoundingMode.HALF_UProunding.
-
-
Constructor Details
-
MathContext
public MathContext(int precision)Constructs a newMathContextwith the specified precision and with the rounding modeHALF_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 newMathContext.- Throws:
IllegalArgumentException- ifprecision < 0.
-
MathContext
Constructs a newMathContextwith 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 newMathContext.roundingMode- the rounding mode for the newMathContext.- Throws:
IllegalArgumentException- ifprecision < 0.NullPointerException- ifroundingModeisnull.
-
MathContext
Constructs a newMathContextfrom 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 thetoString()method.- Throws:
IllegalArgumentException- if the string is not in the correct format or if the precision specified is < 0.
-
-
Method Details
-
getPrecision
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.
-
getRoundingMode
Returns the rounding mode. The rounding mode is the strategy to be used to round results.The rounding mode is one of
RoundingMode.UP,RoundingMode.DOWN,RoundingMode.CEILING,RoundingMode.FLOOR,RoundingMode.HALF_UP,RoundingMode.HALF_DOWN,RoundingMode.HALF_EVEN, orRoundingMode.UNNECESSARY.- Returns:
- the rounding mode.
-
equals
Returns true if x is aMathContextwith the same precision setting and the same rounding mode as thisMathContextinstance.- Overrides:
equalsin classObject- Parameters:
x- object to be compared.- Returns:
trueif thisMathContextinstance is equal to thexargument;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()Returns the hash code for thisMathContextinstance.- Overrides:
hashCodein classObject- Returns:
- the hash code for this
MathContext. - See Also:
Object.equals(java.lang.Object)
-
toString
Returns the string representation for thisMathContextinstance. 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.
-