Interface Rounding
-
public interface Rounding
A convention defining how to round a number.This defines a standard mechanism for rounding a
double
,BigDecimal
orDecimal
. 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 adouble
.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.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method 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.default double
round(double 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.BigDecimal
round(BigDecimal value)
Rounds the specified value according to the rules of the convention.
-
-
-
Method Detail
-
none
static Rounding none()
Obtains an instance that performs no rounding.- Returns:
- the rounding convention
-
of
static Rounding of(Currency currency)
Obtains an instance that rounds to the number of minor units in the currency.This returns a convention that rounds for the specified currency. Rounding follows the normal
RoundingMode.HALF_UP
convention.- Parameters:
currency
- the currency- Returns:
- the rounding convention
-
ofDecimalPlaces
static Rounding ofDecimalPlaces(int decimalPlaces)
Obtains an instance that rounds to the specified number of decimal places.This returns a convention that rounds to the specified number of decimal places. Rounding follows the normal
RoundingMode.HALF_UP
convention.- Parameters:
decimalPlaces
- the number of decimal places to round to, from 0 to 255 inclusive- Returns:
- the rounding convention
- Throws:
IllegalArgumentException
- if the decimal places is invalid
-
ofFractionalDecimalPlaces
static Rounding ofFractionalDecimalPlaces(int decimalPlaces, int fraction)
Obtains an instance from the number of decimal places and 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.
- Parameters:
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 inclusive- Returns:
- the rounding convention
- Throws:
IllegalArgumentException
- if the decimal places or fraction is invalid
-
round
default double round(double value)
Rounds the specified value according to the rules of the convention.- Parameters:
value
- the value to be rounded- Returns:
- the rounded value
-
round
BigDecimal round(BigDecimal value)
Rounds the specified value according to the rules of the convention.- Parameters:
value
- the value to be rounded- Returns:
- the rounded value
-
-