public class BasicRoundingStrategy extends Object implements RoundingStrategy, Serializable
RoundingStrategy
offers to ways to describe a rounding operation: using the decimal
places after (or before) the decimal delimiter and using a rounding step.roundingStep = 10(-numberOfDigits)
numberOfDigits
results in a roundingStep
of 0.0001. To retain 4 digits
after the decimal delimiter, the value is rounded to the next multiple of 0.0001.
Which multiple is chosen - the higher or lower valued one - is determined by the RoundingMode
.
For either parameter - roundingStep
and numberOfDigits
- positive and negative values are allowed.
Positive values of numberOfDigits
designate decimal places after the radix delimiter, negative values
designate decimal places before the radix delimiter. A value of -1, for example, will result in rounding to whole
10s, -2 to whole 100s, and so forth. A value of zero designates integer values.
Using negative values of roundingStep
will yield the same result as a positive roundingStep
of the
same magnitude would have. Using zero as roundingStep
is illegal and will result in an
ArithmeticException
at runtime, when the BasicRoundingStrategy
is used.
MONETARY_ROUNDING, ROUND_ONE
Constructor and Description |
---|
BasicRoundingStrategy(BigDecimal roundingStep,
RoundingMode roundingMode)
Instantiates a new
BasicRoundingStrategy using a rounding step and a rounding mode. |
BasicRoundingStrategy(int numberOfDigits,
RoundingMode roundingMode)
Instantiates a new
BasicRoundingStrategy using the number of digits after (or before) the decimal
delimiter and a rounding mode. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o)
Two instances of BasicRoundingStrategy are equal, if and only if their
roundingStep attributes are
numerically equal and if their roundingMode attributes are equal. |
RoundingMode |
getRoundingMode()
Returns the
RoundingMode used by this instance of BasicRoundingStrategy . |
BigDecimal |
getRoundingStep()
Returns the step, to which is rounded.
|
int |
hashcode()
Returns a hash code for this
RoundingStrategy object. |
int |
hashCode() |
BigDecimal |
round(BigDecimal amount)
Rounds the given
BigDecimal instance amount with a specific strategy. |
public BasicRoundingStrategy(BigDecimal roundingStep, RoundingMode roundingMode)
BasicRoundingStrategy
using a rounding step and a rounding mode. Using both
parameters a value is rounded in the direction specified by roundingMode
to the next multiple of
roundingStep
. Usually, a rounding step is a positive value. However, a negative rounding step can also be
specified, which gives the same results as a positive rounding step of equal value. A roundingStep
of value
zero may not be specified and will result in an ArithmeticException
, if the
RoundingStrategy
is used.roundingStep
- multiple to which is roundedroundingMode
- mode or direction in which is roundedpublic BasicRoundingStrategy(int numberOfDigits, RoundingMode roundingMode)
BasicRoundingStrategy
using the number of digits after (or before) the decimal
delimiter and a rounding mode. roundingMode
specifies the direction in which is rounded, whereas
numberOfDigits
specifies the precision to which is rounded. Positive values for numberOfDigits
describe decimal places after the decimal delimiter, negative values before the decimal delimiter. A value of zero
rounds to integer values.numberOfDigits
- rounding precision in decimal places before (negative) or after (positive) the decimal
delimiterroundingMode
- mode or direction in which is roundedpublic BigDecimal round(BigDecimal amount)
RoundingStrategy
BigDecimal
instance amount
with a specific strategy. Because BigDecimal
is
immutable, a new instance with a rounded value is returned.round
in interface RoundingStrategy
amount
- The BigDecimal
instance which should be rounded.BigDecimal
instance which has a rounded value.public boolean equals(Object o)
Two instances of BasicRoundingStrategy are equal, if and only if their roundingStep
attributes are
numerically equal and if their roundingMode
attributes are equal.
public final int hashcode()
RoundingStrategy
object. The hash code is the exclusive OR of the
roundingStep
, and the hash code of roundingMode
objects.RoundingStrategy
object.public BigDecimal getRoundingStep()
roundingStep
with an
integral result.public RoundingMode getRoundingMode()
RoundingMode
used by this instance of BasicRoundingStrategy
.RoundingMode
used for rounding.Copyright © 2014 Pivotal Software, Inc.. All rights reserved.