public final class HalfUpRounding extends Object implements Rounding, ImmutableBean, Serializable
Rounding
that uses the half-up convention.
This class implements Rounding
to provide the ability to round a number.
Rounding follows the normal RoundingMode.HALF_UP
convention.
For example, this could be used to round a price to the appropriate market convention.
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
.
Modifier and Type | Class and Description |
---|---|
static class |
HalfUpRounding.Meta
The meta-bean for
HalfUpRounding . |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj) |
int |
getDecimalPlaces()
Gets the number of decimal places to round to.
|
int |
getFraction()
Gets the fraction of the smallest decimal place to round to.
|
int |
hashCode() |
static HalfUpRounding.Meta |
meta()
The meta-bean for
HalfUpRounding . |
HalfUpRounding.Meta |
metaBean() |
static HalfUpRounding |
ofDecimalPlaces(int decimalPlaces)
Obtains an instance that rounds to the specified number of decimal places.
|
static HalfUpRounding |
ofFractionalDecimalPlaces(int decimalPlaces,
int fraction)
Obtains an instance from the number of decimal places and fraction.
|
<R> Property<R> |
property(String propertyName) |
Set<String> |
propertyNames() |
BigDecimal |
round(BigDecimal value)
Rounds the specified value according to the rules of the convention.
|
double |
round(double value)
Rounds the specified value according to the rules of the convention.
|
String |
toString() |
public static HalfUpRounding ofDecimalPlaces(int decimalPlaces)
This returns a convention that rounds to the specified number of decimal places.
Rounding follows the normal RoundingMode.HALF_UP
convention.
ofDecimalPlaces
in interface Rounding
decimalPlaces
- the number of decimal places to round to, from 0 to 255 inclusiveIllegalArgumentException
- if the decimal places is invalidpublic static HalfUpRounding 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.
ofFractionalDecimalPlaces
in interface Rounding
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 256 inclusiveIllegalArgumentException
- if the decimal places or fraction is invalidpublic double round(double value)
Rounding
public BigDecimal round(BigDecimal value)
Rounding
public static HalfUpRounding.Meta meta()
HalfUpRounding
.public HalfUpRounding.Meta metaBean()
public Set<String> propertyNames()
propertyNames
in interface Bean
public int getDecimalPlaces()
Rounding follows the normal RoundingMode.HALF_UP
convention.
The value must be from 0 to 255 inclusive.
public int getFraction()
If used, this allows the rounding point to be set as a fraction of the smallest decimal place. For example, setting this field to 32 will round to the nearest 1/32nd of the last decimal place.
This will not be present if rounding is to an exact number of decimal places and there is no fraction. The value must be from 2 to 256 inclusive, 0 is used to indicate no fractional part.
Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.