public interface Rounding
This defines a standard mechanism for rounding a double, BigDecimal or Decimal.
Since financial instruments have different and complex conventions, rounding is extensible.
Note that rounding a double is not straightforward as floating point
numbers are based on a binary representation, not a decimal one.
For example, the value 0.1 cannot be exactly represented in a double.
The standard implementation is HalfUpRounding.
Additional implementations may be added by implementing this interface.
All implementations of this interface must be immutable and thread-safe.
| Modifier and Type | Method and Description |
|---|---|
static Rounding |
none()
Obtains an instance that performs no rounding.
|
static Rounding |
of(Currency currency)
Obtains an instance that rounds to the number of minor units in the currency.
|
static Rounding |
ofDecimalPlaces(int decimalPlaces)
Obtains an instance that rounds to the specified number of decimal places.
|
static Rounding |
ofFractionalDecimalPlaces(int decimalPlaces,
int fraction)
Obtains an instance from the number of decimal places and fraction.
|
BigDecimal |
round(BigDecimal value)
Rounds the specified value according to the rules of the convention.
|
default Decimal |
round(Decimal value)
Rounds the specified value according to the rules of the convention.
|
default double |
round(double value)
Rounds the specified value according to the rules of the convention.
|
static Rounding none()
static Rounding of(Currency currency)
This returns a convention that rounds for the specified currency.
Rounding follows the normal RoundingMode.HALF_UP convention.
currency - the currencystatic Rounding ofDecimalPlaces(int decimalPlaces)
This returns a convention that rounds to the specified number of decimal places.
Rounding follows the normal RoundingMode.HALF_UP convention.
decimalPlaces - the number of decimal places to round to, from 0 to 255 inclusiveIllegalArgumentException - if the decimal places is invalidstatic Rounding ofFractionalDecimalPlaces(int decimalPlaces, int fraction)
This returns a convention that rounds to a fraction of the specified number of decimal places.
Rounding follows the normal RoundingMode.HALF_UP convention.
For example, to round to the nearest 1/32nd of the 4th decimal place, call this method with the arguments 4 and 32.
decimalPlaces - the number of decimal places to round to, from 0 to 255 inclusivefraction - the fraction of the last decimal place, such as 32 for 1/32, from 0 to 255 inclusiveIllegalArgumentException - if the decimal places or fraction is invaliddefault double round(double value)
value - the value to be roundedBigDecimal round(BigDecimal value)
value - the value to be roundedCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.