java.math
Class MathContext

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.math.MathContext
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable

public final class MathContext
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Serializable sample code for java.io.Serializable definition code for java.io.Serializable

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

The base-independent settings are:

  1. precision: the number of digits to be used for an operation; results are rounded to this precision
  2. roundingMode: a RoundingMode sample code for java.math.RoundingMode definition code for java.math.RoundingMode object which specifies the algorithm to be used for rounding.

See Also:
BigDecimal sample code for java.math.BigDecimal definition code for java.math.BigDecimal , RoundingMode sample code for java.math.RoundingMode definition code for java.math.RoundingMode , Serialized Form

Field Summary
static MathContext sample code for java.math.MathContext definition code for java.math.MathContext DECIMAL128 sample code for java.math.MathContext.DECIMAL128 definition code for java.math.MathContext.DECIMAL128
          A MathContext object with a precision setting matching the IEEE 754R Decimal128 format, 34 digits, and a rounding mode of HALF_EVEN sample code for java.math.RoundingMode.HALF_EVEN definition code for java.math.RoundingMode.HALF_EVEN , the IEEE 754R default.
static MathContext sample code for java.math.MathContext definition code for java.math.MathContext DECIMAL32 sample code for java.math.MathContext.DECIMAL32 definition code for java.math.MathContext.DECIMAL32
          A MathContext object with a precision setting matching the IEEE 754R Decimal32 format, 7 digits, and a rounding mode of HALF_EVEN sample code for java.math.RoundingMode.HALF_EVEN definition code for java.math.RoundingMode.HALF_EVEN , the IEEE 754R default.
static MathContext sample code for java.math.MathContext definition code for java.math.MathContext DECIMAL64 sample code for java.math.MathContext.DECIMAL64 definition code for java.math.MathContext.DECIMAL64
          A MathContext object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of HALF_EVEN sample code for java.math.RoundingMode.HALF_EVEN definition code for java.math.RoundingMode.HALF_EVEN , the IEEE 754R default.
static MathContext sample code for java.math.MathContext definition code for java.math.MathContext UNLIMITED sample code for java.math.MathContext.UNLIMITED definition code for java.math.MathContext.UNLIMITED
          A MathContext object whose settings have the values required for unlimited precision arithmetic.
 
Constructor Summary
MathContext sample code for java.math.MathContext.MathContext(int) definition code for java.math.MathContext.MathContext(int) (int setPrecision)
          Constructs a new MathContext with the specified precision and the HALF_UP sample code for java.math.RoundingMode.HALF_UP definition code for java.math.RoundingMode.HALF_UP rounding mode.
MathContext sample code for java.math.MathContext.MathContext(int, java.math.RoundingMode) definition code for java.math.MathContext.MathContext(int, java.math.RoundingMode) (int setPrecision, RoundingMode sample code for java.math.RoundingMode definition code for java.math.RoundingMode  setRoundingMode)
          Constructs a new MathContext with a specified precision and rounding mode.
MathContext sample code for java.math.MathContext.MathContext(java.lang.String) definition code for java.math.MathContext.MathContext(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  val)
          Constructs a new MathContext from a string.
 
Method Summary
 boolean equals sample code for java.math.MathContext.equals(java.lang.Object) definition code for java.math.MathContext.equals(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  x)
          Compares this MathContext with the specified Object for equality.
 int getPrecision sample code for java.math.MathContext.getPrecision() definition code for java.math.MathContext.getPrecision() ()
          Returns the precision setting.
 RoundingMode sample code for java.math.RoundingMode definition code for java.math.RoundingMode getRoundingMode sample code for java.math.MathContext.getRoundingMode() definition code for java.math.MathContext.getRoundingMode() ()
          Returns the roundingMode setting.
 int hashCode sample code for java.math.MathContext.hashCode() definition code for java.math.MathContext.hashCode() ()
          Returns the hash code for this MathContext.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.math.MathContext.toString() definition code for java.math.MathContext.toString() ()
          Returns the string representation of this MathContext.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

UNLIMITED sample code for java.math.MathContext.UNLIMITED

public static final MathContext sample code for java.math.MathContext definition code for java.math.MathContext  UNLIMITED
A MathContext object whose settings have the values required for unlimited precision arithmetic. The values of the settings are: precision=0 roundingMode=HALF_UP


DECIMAL32 sample code for java.math.MathContext.DECIMAL32

public static final MathContext sample code for java.math.MathContext definition code for java.math.MathContext  DECIMAL32
A MathContext object with a precision setting matching the IEEE 754R Decimal32 format, 7 digits, and a rounding mode of HALF_EVEN sample code for java.math.RoundingMode.HALF_EVEN definition code for java.math.RoundingMode.HALF_EVEN , the IEEE 754R default.


DECIMAL64 sample code for java.math.MathContext.DECIMAL64

public static final MathContext sample code for java.math.MathContext definition code for java.math.MathContext  DECIMAL64
A MathContext object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of HALF_EVEN sample code for java.math.RoundingMode.HALF_EVEN definition code for java.math.RoundingMode.HALF_EVEN , the IEEE 754R default.


DECIMAL128 sample code for java.math.MathContext.DECIMAL128

public static final MathContext sample code for java.math.MathContext definition code for java.math.MathContext  DECIMAL128
A MathContext object with a precision setting matching the IEEE 754R Decimal128 format, 34 digits, and a rounding mode of HALF_EVEN sample code for java.math.RoundingMode.HALF_EVEN definition code for java.math.RoundingMode.HALF_EVEN , the IEEE 754R default.

Constructor Detail

MathContext sample code for java.math.MathContext(int) definition code for java.math.MathContext(int)

public MathContext(int setPrecision)
Constructs a new MathContext with the specified precision and the HALF_UP sample code for java.math.RoundingMode.HALF_UP definition code for java.math.RoundingMode.HALF_UP rounding mode.

Parameters:
setPrecision - The non-negative int precision setting.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - setPrecision parameter less than zero.

MathContext sample code for java.math.MathContext(int, java.math.RoundingMode) definition code for java.math.MathContext(int, java.math.RoundingMode)

public MathContext(int setPrecision,
                   RoundingMode sample code for java.math.RoundingMode definition code for java.math.RoundingMode  setRoundingMode)
Constructs a new MathContext with a specified precision and rounding mode.

Parameters:
setPrecision - The non-negative int precision setting.
setRoundingMode - The rounding mode to use.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - setPrecision parameter less than zero.

MathContext sample code for java.math.MathContext(java.lang.String) definition code for java.math.MathContext(java.lang.String)

public MathContext(String sample code for java.lang.String definition code for java.lang.String  val)
Constructs a new MathContext from a string. The string must be in the same format as that produced by the toString() sample code for java.math.MathContext.toString() definition code for java.math.MathContext.toString() method.

An IllegalArgumentException is thrown if the precision section of the string is out of range (< 0) or the string is not in the format created by the toString() sample code for java.math.MathContext.toString() definition code for java.math.MathContext.toString() method.

Parameters:
val - The string to be parsed
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - precision parameter out of range or incorrect format
Method Detail

getPrecision sample code for java.math.MathContext.getPrecision() definition code for java.math.MathContext.getPrecision()

public int getPrecision()
Returns the precision setting. This value is always non-negative.

Returns:
an int which is the value of the precision setting

getRoundingMode sample code for java.math.MathContext.getRoundingMode() definition code for java.math.MathContext.getRoundingMode()

public RoundingMode sample code for java.math.RoundingMode definition code for java.math.RoundingMode  getRoundingMode()
Returns the roundingMode setting. This will be one of RoundingMode.CEILING sample code for java.math.RoundingMode.CEILING definition code for java.math.RoundingMode.CEILING , RoundingMode.DOWN sample code for java.math.RoundingMode.DOWN definition code for java.math.RoundingMode.DOWN , RoundingMode.FLOOR sample code for java.math.RoundingMode.FLOOR definition code for java.math.RoundingMode.FLOOR , RoundingMode.HALF_DOWN sample code for java.math.RoundingMode.HALF_DOWN definition code for java.math.RoundingMode.HALF_DOWN , RoundingMode.HALF_EVEN sample code for java.math.RoundingMode.HALF_EVEN definition code for java.math.RoundingMode.HALF_EVEN , RoundingMode.HALF_UP sample code for java.math.RoundingMode.HALF_UP definition code for java.math.RoundingMode.HALF_UP , RoundingMode.UNNECESSARY sample code for java.math.RoundingMode.UNNECESSARY definition code for java.math.RoundingMode.UNNECESSARY , or RoundingMode.UP sample code for java.math.RoundingMode.UP definition code for java.math.RoundingMode.UP .

Returns:
a RoundingMode object which is the value of the roundingMode setting

equals sample code for java.math.MathContext.equals(java.lang.Object) definition code for java.math.MathContext.equals(java.lang.Object)

public boolean equals(Object sample code for java.lang.Object definition code for java.lang.Object  x)
Compares this MathContext with the specified Object for equality.

Overrides:
equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) in class Object sample code for java.lang.Object definition code for java.lang.Object
Parameters:
x - Object to which this MathContext is to be compared.
Returns:
true if and only if the specified Object is a MathContext object which has exactly the same settings as this object.
See Also:
Object.hashCode() sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable

hashCode sample code for java.math.MathContext.hashCode() definition code for java.math.MathContext.hashCode()

public int hashCode()
Returns the hash code for this MathContext.

Overrides:
hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
hash code for this MathContext
See Also:
Object.equals(java.lang.Object) sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable

toString sample code for java.math.MathContext.toString() definition code for java.math.MathContext.toString()

public String sample code for java.lang.String definition code for java.lang.String  toString()
Returns the string representation of this MathContext. The String returned represents the settings of the 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 "precision=", immediately followed by the value of the precision setting as a numeric string as if generated by the Integer.toString sample code for java.lang.Integer.toString(int) definition code for java.lang.Integer.toString(int) method.
  2. The string "roundingMode=", immediately followed by the value of the roundingMode setting as a word. This word will be the same as the name of the corresponding public constant in the RoundingMode sample code for java.math.RoundingMode definition code for java.math.RoundingMode enum.

For example:

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

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a String representing the context settings.