MathContext immutable class encapsulates the
settings understood by the operator methods of the BigDecimal
class (and potentially other classes). Operator methods are those
that effect an operation on a number or a pair of numbers.
The settings, which are not base-dependent, comprise:
digits:
the number of digits (precision) to be used for an operation
form:
the form of any exponent that results from the operation
lostDigits:
whether checking for lost digits is enabled
roundingMode:
the algorithm to be used for rounding.
When provided, a MathContext object supplies the
settings for an operation directly.
When MathContext.DEFAULT is provided for a
MathContext parameter then the default settings are used
(9, SCIENTIFIC, false, ROUND_HALF_UP).
In the BigDecimal class, all methods which accept a
MathContext object defaults) also have a version of the
method which does not accept a MathContext parameter. These versions
carry out unlimited precision fixed point arithmetic (as though the
settings were (0, PLAIN, false, ROUND_HALF_UP).
The instance variables are shared with default access (so they are
directly accessible to the BigDecimal class), but must
never be changed.
The rounding mode constants have the same names and values as the
constants of the same name in java.math.BigDecimal, to
maintain compatibility with earlier versions of
BigDecimal.
BigDecimal
| Modifier and Type | Field and Description |
|---|---|
| public static final MathContext | DEFAULT
A |
| private static final int | |
| private static final int | |
| private static final boolean | |
| private static final int | |
| pack-priv int | digits
The number of digits (precision) to be used for an operation. |
| public static final int | ENGINEERING
Standard floating point notation (with engineering exponential format, where the power of ten is a multiple of 3). |
| pack-priv int | form
The form of results from an operation. |
| pack-priv boolean | lostDigits
Controls whether lost digits checking is enabled for an operation. |
| private static final int | |
| private static final int | |
| public static final int | PLAIN
Plain (fixed point) notation, without any exponent. |
| public static final int | ROUND_CEILING
Rounding mode to round to a more positive number. |
| public static final int | ROUND_DOWN
Rounding mode to round towards zero. |
| public static final int | ROUND_FLOOR
Rounding mode to round to a more negative number. |
| public static final int | ROUND_HALF_DOWN
Rounding mode to round to nearest neighbor, where an equidistant value is rounded down. |
| public static final int | ROUND_HALF_EVEN
Rounding mode to round to nearest neighbor, where an equidistant value is rounded to the nearest even neighbor. |
| public static final int | ROUND_HALF_UP
Rounding mode to round to nearest neighbor, where an equidistant value is rounded up. |
| public static final int | ROUND_UNNECESSARY
Rounding mode to assert that no rounding is necessary. |
| public static final int | ROUND_UP
Rounding mode to round away from zero. |
| pack-priv int | roundingMode
The rounding algorithm to be used for an operation. |
| private static final int[] | |
| private static final String[] | |
| public static final int | SCIENTIFIC
Standard floating point notation (with scientific exponential format, where there is one digit before any decimal point). |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public | MathContext(int
The setdigits)int digits setting
for this MathContext.Constructs a new |
| public | MathContext(int
The setdigits, int int digits setting
for this MathContext.The setform)int form setting
for this MathContext.Constructs a new |
| public | MathContext(int
The setdigits, int int digits setting
for this MathContext.The setform, boolean int form setting
for this MathContext.The setlostdigits)boolean lostDigits
setting for this MathContext.Constructs a new |
| public | MathContext(int
The setdigits, int int digits setting
for this MathContext.The setform, boolean int form setting
for this MathContext.The setlostdigits, int boolean lostDigits
setting for this MathContext.The setroundingmode)int roundingMode setting
for this MathContext.Constructs a new |
| Modifier and Type | Method and Description |
|---|---|
| public int | |
| public int | |
| public boolean | Returns: aboolean which is the value of the lostDigits
settingReturns the lostDigits setting. |
| public int | Returns: anint which is the value of the roundingMode
settingReturns the roundingMode setting. |
| private static boolean | |
| public String | Returns: aString representing the context settings.Overrides java. MathContext as a readable string.
|
| DEFAULT | back to summary |
|---|---|
| public static final MathContext DEFAULT A
| |
| DEFAULT_DIGITS | back to summary |
|---|---|
| private static final int DEFAULT_DIGITS | |
| DEFAULT_FORM | back to summary |
|---|---|
| private static final int DEFAULT_FORM | |
| DEFAULT_LOSTDIGITS | back to summary |
|---|---|
| private static final boolean DEFAULT_LOSTDIGITS | |
| DEFAULT_ROUNDINGMODE | back to summary |
|---|---|
| private static final int DEFAULT_ROUNDINGMODE | |
| digits | back to summary |
|---|---|
| pack-priv int digits The number of digits (precision) to be used for an operation. A value of 0 indicates that unlimited precision (as many digits as are required) will be used.
The
| |
| ENGINEERING | back to summary |
|---|---|
| public static final int ENGINEERING Standard floating point notation (with engineering exponential
format, where the power of ten is a multiple of 3).
Used as a setting to control the form of the result of a
| |
| form | back to summary |
|---|---|
| pack-priv int form The form of results from an operation.
The
| |
| lostDigits | back to summary |
|---|---|
| pack-priv boolean lostDigits Controls whether lost digits checking is enabled for an
operation.
Set to
When enabled, the | |
| MAX_DIGITS | back to summary |
|---|---|
| private static final int MAX_DIGITS | |
| MIN_DIGITS | back to summary |
|---|---|
| private static final int MIN_DIGITS | |
| PLAIN | back to summary |
|---|---|
| public static final int PLAIN Plain (fixed point) notation, without any exponent.
Used as a setting to control the form of the result of a
| |
| ROUND_CEILING | back to summary |
|---|---|
| public static final int ROUND_CEILING Rounding mode to round to a more positive number.
Used as a setting to control the rounding mode used during a
If any of the discarded digits are non-zero then the result should be rounded towards the next more positive digit.
| |
| ROUND_DOWN | back to summary |
|---|---|
| public static final int ROUND_DOWN Rounding mode to round towards zero.
Used as a setting to control the rounding mode used during a
All discarded digits are ignored (truncated). The result is neither incremented nor decremented.
| |
| ROUND_FLOOR | back to summary |
|---|---|
| public static final int ROUND_FLOOR Rounding mode to round to a more negative number.
Used as a setting to control the rounding mode used during a
If any of the discarded digits are non-zero then the result should be rounded towards the next more negative digit.
| |
| ROUND_HALF_DOWN | back to summary |
|---|---|
| public static final int ROUND_HALF_DOWN Rounding mode to round to nearest neighbor, where an equidistant
value is rounded down.
Used as a setting to control the rounding mode used during a
If the discarded digits represent greater than half (0.5 times) the value of a one in the next position then the result should be rounded up (away from zero). Otherwise the discarded digits are ignored.
| |
| ROUND_HALF_EVEN | back to summary |
|---|---|
| public static final int ROUND_HALF_EVEN Rounding mode to round to nearest neighbor, where an equidistant
value is rounded to the nearest even neighbor.
Used as a setting to control the rounding mode used during a
If the discarded digits represent greater than half (0.5 times) the value of a one in the next position then the result should be rounded up (away from zero). If they represent less than half, then the result should be rounded down. Otherwise (they represent exactly half) the result is rounded down if its rightmost digit is even, or rounded up if its rightmost digit is odd (to make an even digit).
| |
| ROUND_HALF_UP | back to summary |
|---|---|
| public static final int ROUND_HALF_UP Rounding mode to round to nearest neighbor, where an equidistant
value is rounded up.
Used as a setting to control the rounding mode used during a
If the discarded digits represent greater than or equal to half (0.5 times) the value of a one in the next position then the result should be rounded up (away from zero). Otherwise the discarded digits are ignored.
| |
| ROUND_UNNECESSARY | back to summary |
|---|---|
| public static final int ROUND_UNNECESSARY Rounding mode to assert that no rounding is necessary.
Used as a setting to control the rounding mode used during a
Rounding (potential loss of information) is not permitted.
If any of the discarded digits are non-zero then an
| |
| ROUND_UP | back to summary |
|---|---|
| public static final int ROUND_UP Rounding mode to round away from zero.
Used as a setting to control the rounding mode used during a
If any of the discarded digits are non-zero then the result will be rounded up (away from zero).
| |
| roundingMode | back to summary |
|---|---|
| pack-priv int roundingMode The rounding algorithm to be used for an operation.
The | |
| ROUNDS | back to summary |
|---|---|
| private static final int[] ROUNDS | |
| ROUNDWORDS | back to summary |
|---|---|
| private static final String[] ROUNDWORDS | |
| SCIENTIFIC | back to summary |
|---|---|
| public static final int SCIENTIFIC Standard floating point notation (with scientific exponential
format, where there is one digit before any decimal point).
Used as a setting to control the form of the result of a
| |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| MathContext | back to summary |
|---|---|
| public MathContext(int setdigits) Constructs a new
| |
| MathContext | back to summary |
|---|---|
| public MathContext(int setdigits, int setform) Constructs a new
| |
| MathContext | back to summary |
|---|---|
| public MathContext(int setdigits, int setform, boolean setlostdigits) Constructs a new
| |
| MathContext | back to summary |
|---|---|
| public MathContext(int setdigits, int setform, boolean setlostdigits, int setroundingmode) Constructs a new
| |
| getDigits | back to summary |
|---|---|
| public int getDigits() Returns the digits setting. This value is always non-negative.
| |
| getForm | back to summary |
|---|---|
| public int getForm() Returns the form setting.
This will be one of
| |
| getLostDigits | back to summary |
|---|---|
| public boolean getLostDigits() Returns the lostDigits setting.
This will be either
| |
| getRoundingMode | back to summary |
|---|---|
| public int getRoundingMode() Returns the roundingMode setting.
This will be one of
| |
| isValidRound | back to summary |
|---|---|
| private static boolean isValidRound(int testround) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns the
For example:
Additional words may be appended to the result of
| |