public abstract class Rounding
extends java.lang.Object
implements java.lang.Cloneable
Rounding is an abstract class for all rounding strategies.
The primary method of this class is round
. This
method will round a BigDecimal value to the appropriate integer
and return a BigInteger object.
BigDecimal
objects are rounded by calling the
BigDecimal.setScale(int)
method with the appropriate
rounding mode. Several static methods are provided in this class
to support this functionality for each of the supported rounding
modes. In addition, several static singleton rounding strategies
are provided by this class to implement one of the supported
routing modes. Each of these rounding strategies are modeled
after the rounding strategies provided by BigDecimal
and include:
roundUp
method and the Rounding
singletons GENERAL
, UNKNOWN
and UP
.roundDown
method and the Rounding
singleton DOWN
.roundFloor
method and the Rounding
singleton FLOOR
.roundCeiling
method and the Rounding
singleton CEILING
.roundHalfUp
method and the Rounding
singleton HALF_UP
.roundHalfDown
method and the Rounding
singleton HALF_DOWN
.roundHalfEven
method and the Rounding
singletons HALF_EVEN
and CONVERGENT
.roundHalfFloor
method and
the Rounding singleton HALF_FLOOR
.roundHalfCeiling
method and
the Rounding singleton HALF_CEILING
.A specific strategy may be chosen dynamically by invoking forName() or getName() with one of the above strategy names. Alternatively a strategy may be selected by using one of the static singletons.
The truncate and nearest strategies should be preferred since they correspond to capabilities available on many processors. Other rounding strategies may require costly code on practical hardware.
The active class functionality is provided by the quantize method which is normally invoked from Quantization.quantize.
Red |
Red (Ed.Willink) |
Modifier and Type | Class and Description |
---|---|
static class |
Rounding.RoundCeiling
Rounding class implementing the round ceiling strategy.
|
static class |
Rounding.RoundDown
Rounding class implementing the round down strategy.
|
static class |
Rounding.RoundFloor
Rounding class implementing the round floor strategy.
|
static class |
Rounding.RoundHalfCeiling
Rounding class implementing the round half ceiling strategy.
|
static class |
Rounding.RoundHalfDown
Rounding class implementing the round half down strategy.
|
static class |
Rounding.RoundHalfEven
Rounding class implementing the round half even strategy.
|
static class |
Rounding.RoundHalfFloor
Rounding class implementing the round half floor strategy.
|
static class |
Rounding.RoundHalfUp
Rounding class implementing the round half up strategy.
|
static class |
Rounding.RoundUp
Rounding class implementing the round up strategy.
|
Modifier and Type | Field and Description |
---|---|
static Rounding.RoundCeiling |
CEILING
Singleton implementing ceiling rounding strategy.
|
static Rounding.RoundHalfEven |
CONVERGENT
Singleton implementing convergent rounding strategy.
|
static Rounding.RoundDown |
DOWN
Singleton implementing down rounding strategy.
|
static Rounding.RoundFloor |
FLOOR
Singleton implementing floor rounding strategy.
|
static Rounding |
GENERAL
Singleton implementing general rounding strategy.
|
static Rounding.RoundHalfCeiling |
HALF_CEILING
Singleton implementing half ceiling rounding strategy.
|
static Rounding.RoundHalfDown |
HALF_DOWN
Singleton implementing half down rounding strategy.
|
static Rounding.RoundHalfEven |
HALF_EVEN
Singleton implementing half even rounding strategy.
|
static Rounding.RoundHalfFloor |
HALF_FLOOR
Singleton implementing half floor rounding strategy.
|
static Rounding.RoundHalfUp |
HALF_UP
Singleton implementing half up rounding strategy.
|
static Rounding.RoundHalfCeiling |
NEAREST
Singleton implementing nearest rounding strategy.
|
static Rounding.RoundFloor |
TRUNCATE
Singleton implementing truncate rounding strategy.
|
static Rounding |
UNKNOWN
Singleton implementing unknown rounding strategy.
|
static Rounding |
UNNECESSARY
Singleton implementing unnecessary rounding strategy.
|
static Rounding.RoundUp |
UP
Singleton implementing up rounding strategy.
|
Modifier | Constructor and Description |
---|---|
protected |
Rounding(java.lang.String name)
Construct a Rounding object with the given String name.
|
Modifier and Type | Method and Description |
---|---|
(package private) static void |
_addRounding(Rounding type,
java.lang.String name) |
java.lang.Object |
clone()
Return this, that is, return the reference to this object.
|
boolean |
equals(java.lang.Object object)
Determine if the argument represents the same Rounding as this
object.
|
static Rounding |
forName(java.lang.String name)
Return an instance of this class with the specified name.
|
static Rounding |
getName(java.lang.String name)
Return an instance of this class with the specified name,
or null if none exists.
|
int |
hashCode()
Return a hash code value for this object.
|
static java.util.Iterator |
nameIterator()
Return an iterator for the names of all overflow types.
|
abstract java.math.BigInteger |
round(java.math.BigDecimal decimal)
Round the BigDecimal value using the appropriate rounding
strategy.
|
static java.math.BigDecimal |
roundCeiling(java.math.BigDecimal decimal)
Rounding mode to round towards positive infinity.
|
static java.math.BigDecimal |
roundDown(java.math.BigDecimal decimal)
Rounding mode to round towards zero.
|
static java.math.BigDecimal |
roundFloor(java.math.BigDecimal decimal)
Rounding mode to round towards negative infinity.
|
static java.math.BigDecimal |
roundHalfCeiling(java.math.BigDecimal decimal)
Rounding mode to round towards "nearest neighbor" unless
both neighbors are equidistant, in which case round
"ceiling".
|
static java.math.BigDecimal |
roundHalfDown(java.math.BigDecimal decimal)
Rounding mode to round towards "nearest neighbor" unless
both neighbors are equidistant, in which case round down.
|
static java.math.BigDecimal |
roundHalfEven(java.math.BigDecimal decimal)
Rounding mode to round towards the "nearest neighbor" unless
both neighbors are equidistant, in which case, round towards
the even neighbor.
|
static java.math.BigDecimal |
roundHalfFloor(java.math.BigDecimal decimal)
Rounding mode to round towards "nearest neighbor" unless
both neighbors are equidistant, in which case round
"ceiling".
|
static java.math.BigDecimal |
roundHalfUp(java.math.BigDecimal decimal)
Rounding mode to round towards "nearest neighbor" unless
both neighbors are equidistant, in which case round up.
|
static java.math.BigDecimal |
roundUp(java.math.BigDecimal decimal)
Rounding mode to round away from zero.
|
java.lang.String |
toString()
Return the string representation of this rounding.
|
public static final Rounding.RoundCeiling CEILING
public static final Rounding.RoundFloor FLOOR
public static final Rounding.RoundFloor TRUNCATE
public static final Rounding.RoundDown DOWN
public static final Rounding.RoundUp UP
public static final Rounding.RoundHalfDown HALF_DOWN
public static final Rounding.RoundHalfUp HALF_UP
public static final Rounding.RoundHalfEven HALF_EVEN
public static final Rounding.RoundHalfEven CONVERGENT
public static final Rounding.RoundHalfCeiling HALF_CEILING
public static final Rounding.RoundHalfCeiling NEAREST
public static final Rounding.RoundHalfFloor HALF_FLOOR
public static final Rounding GENERAL
public static final Rounding UNKNOWN
public static final Rounding UNNECESSARY
protected Rounding(java.lang.String name)
name
- The String name to give this Rounding strategy.public java.lang.Object clone()
clone
in class java.lang.Object
public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- Another object.public static Rounding forName(java.lang.String name)
name
- Rounding mode name.public static Rounding getName(java.lang.String name) throws java.lang.IllegalArgumentException
name
- The name of the Rounding strategy to find.java.lang.IllegalArgumentException
- If the string does not
match one of the known strategies.public int hashCode()
hashCode
in class java.lang.Object
public static java.util.Iterator nameIterator()
public abstract java.math.BigInteger round(java.math.BigDecimal decimal)
decimal
- value to be roundedpublic static java.math.BigDecimal roundCeiling(java.math.BigDecimal decimal)
roundUp(java.math.BigDecimal)
;
if decimal is negative, behave as roundDown(java.math.BigDecimal)
.decimal
- The BigDecimal value to round.BigDecimal.ROUND_CEILING
public static java.math.BigDecimal roundDown(java.math.BigDecimal decimal)
decimal
- The BigDecimal value to round.BigDecimal.ROUND_DOWN
public static java.math.BigDecimal roundFloor(java.math.BigDecimal decimal)
roundDown(java.math.BigDecimal)
;
if decimal is negative, behave as roundUp(java.math.BigDecimal)
.decimal
- The BigDecimal value to round.BigDecimal.ROUND_FLOOR
public static java.math.BigDecimal roundHalfCeiling(java.math.BigDecimal decimal)
Note that there is no half ceiling rounding mode
supported for BigDecimal values. This method uses a
combination of the BigDecimal.ROUND_HALF_UP
and
BigDecimal.ROUND_HALF_DOWN
to perform this
new rounding mode.
decimal
- The BigDecimal value to round.public static java.math.BigDecimal roundHalfDown(java.math.BigDecimal decimal)
decimal
- The BigDecimal value to round.BigDecimal.ROUND_HALF_UP
public static java.math.BigDecimal roundHalfEven(java.math.BigDecimal decimal)
decimal
- The BigDecimal value to round.BigDecimal.ROUND_HALF_EVEN
public static java.math.BigDecimal roundHalfFloor(java.math.BigDecimal decimal)
Note that there is no half floor rounding mode
supported for BigDecimal values. This method uses a
combination of the BigDecimal.ROUND_HALF_UP
and
BigDecimal.ROUND_HALF_DOWN
to perform this
new rounding mode.
decimal
- The BigDecimal value to round.public static java.math.BigDecimal roundHalfUp(java.math.BigDecimal decimal)
decimal
- The BigDecimal value to round.BigDecimal.ROUND_HALF_UP
public static java.math.BigDecimal roundUp(java.math.BigDecimal decimal)
decimal
- The BigDecimal value to round.BigDecimal.ROUND_UP
public java.lang.String toString()
toString
in class java.lang.Object
static void _addRounding(Rounding type, java.lang.String name)