public final class MathContext extends Object implements Serializable
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
,
Serialized FormModifier and Type | Field and Description |
---|---|
static MathContext |
DEFAULT
A
MathContext object initialized to the default
settings for general-purpose arithmetic. |
static int |
ENGINEERING
Standard floating point notation (with engineering exponential
format, where the power of ten is a multiple of 3).
|
static int |
PLAIN
Plain (fixed point) notation, without any exponent.
|
static int |
ROUND_CEILING
Rounding mode to round to a more positive number.
|
static int |
ROUND_DOWN
Rounding mode to round towards zero.
|
static int |
ROUND_FLOOR
Rounding mode to round to a more negative number.
|
static int |
ROUND_HALF_DOWN
Rounding mode to round to nearest neighbor, where an equidistant
value is rounded down.
|
static int |
ROUND_HALF_EVEN
Rounding mode to round to nearest neighbor, where an equidistant
value is rounded to the nearest even neighbor.
|
static int |
ROUND_HALF_UP
Rounding mode to round to nearest neighbor, where an equidistant
value is rounded up.
|
static int |
ROUND_UNNECESSARY
Rounding mode to assert that no rounding is necessary.
|
static int |
ROUND_UP
Rounding mode to round away from zero.
|
static int |
SCIENTIFIC
Standard floating point notation (with scientific exponential
format, where there is one digit before any decimal point).
|
Constructor and Description |
---|
MathContext(int setdigits)
Constructs a new
MathContext with a specified
precision. |
MathContext(int setdigits,
int setform)
Constructs a new
MathContext with a specified
precision and form. |
MathContext(int setdigits,
int setform,
boolean setlostdigits)
Constructs a new
MathContext with a specified
precision, form, and lostDigits setting. |
MathContext(int setdigits,
int setform,
boolean setlostdigits,
int setroundingmode)
Constructs a new
MathContext with a specified
precision, form, lostDigits, and roundingMode setting. |
Modifier and Type | Method and Description |
---|---|
int |
getDigits()
Returns the digits setting.
|
int |
getForm()
Returns the form setting.
|
boolean |
getLostDigits()
Returns the lostDigits setting.
|
int |
getRoundingMode()
Returns the roundingMode setting.
|
String |
toString()
Returns the
MathContext as a readable string. |
public static final int PLAIN
BigDecimal
operation.
A zero result in plain form may have a decimal part of one or
more zeros.ENGINEERING
,
SCIENTIFIC
,
Constant Field Valuespublic static final int SCIENTIFIC
BigDecimal
operation.
A zero result in plain form may have a decimal part of one or
more zeros.ENGINEERING
,
PLAIN
,
Constant Field Valuespublic static final int ENGINEERING
BigDecimal
operation.
A zero result in plain form may have a decimal part of one or
more zeros.PLAIN
,
SCIENTIFIC
,
Constant Field Valuespublic static final int ROUND_CEILING
BigDecimal
operation.
If any of the discarded digits are non-zero then the result should be rounded towards the next more positive digit.
public static final int ROUND_DOWN
BigDecimal
operation.
All discarded digits are ignored (truncated). The result is neither incremented nor decremented.
public static final int ROUND_FLOOR
BigDecimal
operation.
If any of the discarded digits are non-zero then the result should be rounded towards the next more negative digit.
public static final int ROUND_HALF_DOWN
BigDecimal
operation.
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.
public static final int ROUND_HALF_EVEN
BigDecimal
operation.
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).
public static final int ROUND_HALF_UP
BigDecimal
operation.
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.
public static final int ROUND_UNNECESSARY
BigDecimal
operation.
Rounding (potential loss of information) is not permitted.
If any of the discarded digits are non-zero then an
ArithmeticException
should be thrown.
public static final int ROUND_UP
BigDecimal
operation.
If any of the discarded digits are non-zero then the result will be rounded up (away from zero).
public static final MathContext DEFAULT
MathContext
object initialized to the default
settings for general-purpose arithmetic. That is,
digits=9 form=SCIENTIFIC lostDigits=false
roundingMode=ROUND_HALF_UP
.SCIENTIFIC
,
ROUND_HALF_UP
public MathContext(int setdigits)
MathContext
with a specified
precision.
The other settings are set to the default values
(see DEFAULT
).
An IllegalArgumentException
is thrown if the
setdigits
parameter is out of range
(<0 or >999999999).setdigits
- The int
digits setting
for this MathContext
.IllegalArgumentException
- parameter out of range.public MathContext(int setdigits, int setform)
MathContext
with a specified
precision and form.
The other settings are set to the default values
(see DEFAULT
).
An IllegalArgumentException
is thrown if the
setdigits
parameter is out of range
(<0 or >999999999), or if the value given for the
setform
parameter is not one of the appropriate
constants.setdigits
- The int
digits setting
for this MathContext
.setform
- The int
form setting
for this MathContext
.IllegalArgumentException
- parameter out of range.public MathContext(int setdigits, int setform, boolean setlostdigits)
MathContext
with a specified
precision, form, and lostDigits setting.
The roundingMode setting is set to its default value
(see DEFAULT
).
An IllegalArgumentException
is thrown if the
setdigits
parameter is out of range
(<0 or >999999999), or if the value given for the
setform
parameter is not one of the appropriate
constants.setdigits
- The int
digits setting
for this MathContext
.setform
- The int
form setting
for this MathContext
.setlostdigits
- The boolean
lostDigits
setting for this MathContext
.IllegalArgumentException
- parameter out of range.public MathContext(int setdigits, int setform, boolean setlostdigits, int setroundingmode)
MathContext
with a specified
precision, form, lostDigits, and roundingMode setting.
An IllegalArgumentException
is thrown if the
setdigits
parameter is out of range
(<0 or >999999999), or if the value given for the
setform
or setroundingmode
parameters is
not one of the appropriate constants.setdigits
- The int
digits setting
for this MathContext
.setform
- The int
form setting
for this MathContext
.setlostdigits
- The boolean
lostDigits
setting for this MathContext
.setroundingmode
- The int
roundingMode setting
for this MathContext
.IllegalArgumentException
- parameter out of range.public int getDigits()
int
which is the value of the digits
settingpublic int getForm()
int
which is the value of the form settingpublic boolean getLostDigits()
true
(enabled) or
false
(disabled).boolean
which is the value of the lostDigits
settingpublic int getRoundingMode()
ROUND_CEILING
,
ROUND_DOWN
,
ROUND_FLOOR
,
ROUND_HALF_DOWN
,
ROUND_HALF_EVEN
,
ROUND_HALF_UP
,
ROUND_UNNECESSARY
, or
ROUND_UP
.int
which is the value of the roundingMode
settingpublic String toString()
MathContext
as a readable string.
The String
returned represents the settings of the
MathContext
object as four blank-delimited words
separated by a single blank and with no leading or trailing blanks,
as follows:
digits=
, immediately followed by
the value of the digits setting as a numeric word.
form=
, immediately followed by
the value of the form setting as an uppercase word
(one of SCIENTIFIC
, PLAIN
, or
ENGINEERING
).
lostDigits=
, immediately followed by
the value of the lostDigits setting
(1
if enabled, 0
if disabled).
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.
For example:
digits=9 form=SCIENTIFIC lostDigits=0 roundingMode=ROUND_HALF_UP
Additional words may be appended to the result of
toString
in the future if more properties are added
to the class.
Copyright © 2016 Unicode, Inc. and others.