Save This Page
Home » openjdk-7 » java » math » [javadoc | source]
java.math
public final class: MathContext [javadoc | source]
java.lang.Object
   java.math.MathContext

All Implemented Interfaces:
    Serializable

Immutable objects which encapsulate the context settings which describe certain rules for numerical operators, such as those implemented by the BigDecimal class.

The base-independent settings are:

  1. {@code precision}: the number of digits to be used for an operation; results are rounded to this precision
  2. {@code roundingMode}: a RoundingMode object which specifies the algorithm to be used for rounding.
Field Summary
public static final  MathContext UNLIMITED    A {@code MathContext} object whose settings have the values required for unlimited precision arithmetic. The values of the settings are: precision=0 roundingMode=HALF_UP  
public static final  MathContext DECIMAL32    A {@code MathContext} object with a precision setting matching the IEEE 754R Decimal32 format, 7 digits, and a rounding mode of HALF_EVEN , the IEEE 754R default. 
public static final  MathContext DECIMAL64    A {@code MathContext} object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of HALF_EVEN , the IEEE 754R default. 
public static final  MathContext DECIMAL128    A {@code MathContext} object with a precision setting matching the IEEE 754R Decimal128 format, 34 digits, and a rounding mode of HALF_EVEN , the IEEE 754R default. 
final  int precision    The number of digits to be used for an operation. A value of 0 indicates that unlimited precision (as many digits as are required) will be used. Note that leading zeros (in the coefficient of a number) are never significant.

{@code precision} will always be non-negative.

    serial:
 
final  RoundingMode roundingMode    The rounding algorithm to be used for an operation. 
transient  BigInteger roundingMax    Lookaside for the rounding points (the numbers which determine whether the coefficient of a number will require rounding). These will be present if {@code precision > 0} and {@code precision <= MAX_LOOKASIDE}. In this case they will share the {@code BigInteger int[]} array. Note that the transients cannot be {@code final} because they are reconstructed on deserialization. 
transient  BigInteger roundingMin     
Constructor:
 public MathContext(int setPrecision) 
    Parameters:
    setPrecision - The non-negative {@code int} precision setting.
    Throws:
    IllegalArgumentException - if the {@code setPrecision} parameter is less than zero.
 public MathContext(String val) 
    Constructs a new {@code MathContext} from a string. The string must be in the same format as that produced by the #toString method.

    An {@code IllegalArgumentException} is thrown if the precision section of the string is out of range ({@code < 0}) or the string is not in the format created by the #toString method.

    Parameters:
    val - The string to be parsed
    Throws:
    IllegalArgumentException - if the precision section is out of range or of incorrect format
    NullPointerException - if the argument is {@code null}
 public MathContext(int setPrecision,
    RoundingMode setRoundingMode) 
    Constructs a new {@code MathContext} with a specified precision and rounding mode.
    Parameters:
    setPrecision - The non-negative {@code int} precision setting.
    setRoundingMode - The rounding mode to use.
    Throws:
    IllegalArgumentException - if the {@code setPrecision} parameter is less than zero.
    NullPointerException - if the rounding mode argument is {@code null}
Method from java.math.MathContext Summary:
equals,   getPrecision,   getRoundingMode,   hashCode,   toString
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.math.MathContext Detail:
 public boolean equals(Object x) 
    Compares this {@code MathContext} with the specified {@code Object} for equality.
 public int getPrecision() 
    Returns the {@code precision} setting. This value is always non-negative.
 public RoundingMode getRoundingMode() 
 public int hashCode() 
    Returns the hash code for this {@code MathContext}.
 public String toString() 
    Returns the string representation of this {@code MathContext}. The {@code String} returned represents the settings of the {@code MathContext} object as two space-delimited words (separated by a single space character, '\u0020', and with no leading or trailing white space), as follows:
    1. The string {@code "precision="}, immediately followed by the value of the precision setting as a numeric string as if generated by the Integer.toString method.
    2. The string {@code "roundingMode="}, immediately followed by the value of the {@code roundingMode} setting as a word. This word will be the same as the name of the corresponding public constant in the RoundingMode enum.

    For example:

    precision=9 roundingMode=HALF_UP
    
    Additional words may be appended to the result of {@code toString} in the future if more properties are added to this class.