public enum DecimalRounding extends Enum<DecimalRounding>
RoundingMode
and implements the
functionality to actually perform such rounding.Enum Constant and Description |
---|
CEILING
Rounding mode to round towards positive infinity.
|
DOWN
Rounding mode to round towards zero.
|
FLOOR
Rounding mode to round towards negative infinity.
|
HALF_DOWN
Rounding mode to round towards "nearest neighbor" unless both
neighbors are equidistant, in which case round down.
|
HALF_EVEN
Rounding mode to round towards the "nearest neighbor" unless
both neighbors are equidistant, in which case, round towards the even
neighbor.
|
HALF_UP
Rounding mode to round towards "nearest neighbor" unless both
neighbors are equidistant, in which case round up.
|
UNNECESSARY
Rounding mode to assert that the requested operation has an exact result,
hence no rounding is necessary.
|
UP
Rounding mode to round away from zero.
|
Modifier and Type | Field and Description |
---|---|
static Set<DecimalRounding> |
VALUES
Immutable set with all values of this enum.
|
Modifier and Type | Method and Description |
---|---|
abstract int |
calculateRoundingIncrement(int sign,
long truncatedValue,
TruncatedPart truncatedPart)
Returns the rounding increment appropriate for this decimal rounding.
|
abstract RoundingMode |
getRoundingMode()
Returns the
RoundingMode associated with this decimal rounding
constant. |
static DecimalRounding |
valueOf(RoundingMode roundingMode)
Returns the decimal rounding constant for the given rounding mode.
|
static DecimalRounding |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static DecimalRounding[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final DecimalRounding UP
RoundingMode.UP
public static final DecimalRounding DOWN
RoundingMode.DOWN
public static final DecimalRounding CEILING
RoundingMode.UP
; if negative, behaves as
for RoundingMode.DOWN
. Note that this rounding mode never
decreases the calculated value.RoundingMode.CEILING
public static final DecimalRounding FLOOR
RoundingMode.DOWN
; if negative, behave as
for RoundingMode.UP
. Note that this rounding mode never increases
the calculated value.RoundingMode.FLOOR
public static final DecimalRounding HALF_UP
RoundingMode.UP
if the discarded fraction is ≥ 0.5; otherwise,
behaves as for RoundingMode.DOWN
. Note that this is the rounding
mode commonly taught at school.RoundingMode.HALF_UP
public static final DecimalRounding HALF_DOWN
RoundingMode.UP
if the discarded fraction is > 0.5; otherwise,
behaves as for RoundingMode.DOWN
.RoundingMode.HALF_DOWN
public static final DecimalRounding HALF_EVEN
RoundingMode.HALF_UP
if the digit to the
left of the discarded fraction is odd; behaves as for
RoundingMode.HALF_DOWN
if it's even. Note that this is the
rounding mode that statistically minimizes cumulative error when applied
repeatedly over a sequence of calculations. It is sometimes known as
"Banker's rounding," and is chiefly used in the USA. This
rounding mode is analogous to the rounding policy used for float
and double
arithmetic in Java.RoundingMode.HALF_EVEN
public static final DecimalRounding UNNECESSARY
ArithmeticException
is thrown.RoundingMode.UNNECESSARY
public static final Set<DecimalRounding> VALUES
values()
.public static DecimalRounding[] values()
for (DecimalRounding c : DecimalRounding.values()) System.out.println(c);
public static DecimalRounding valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic abstract RoundingMode getRoundingMode()
RoundingMode
associated with this decimal rounding
constant.public abstract int calculateRoundingIncrement(int sign, long truncatedValue, TruncatedPart truncatedPart)
sign
- the sign of the total value, either +1 or -1; determines the
result value if roundedtruncatedValue
- the truncated result before rounding is appliedtruncatedPart
- classification of the trunctated part of the valuetruncatedValue
to get the rounded
result, one of -1, 0 or 1public static final DecimalRounding valueOf(RoundingMode roundingMode)
roundingMode
- the rounding mode