com.opengamma.strata.basics.value

## Class HalfUpRounding

• All Implemented Interfaces:
Rounding, Serializable, Bean, ImmutableBean

public final class HalfUpRounding
extends Object
implements Rounding, ImmutableBean, Serializable
Standard implementation of 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.

Serialized Form
• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
static class  HalfUpRounding.Meta
The meta-bean for HalfUpRounding.
• ### Method Summary

All Methods
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()
• ### Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait
• ### Methods inherited from interface com.opengamma.strata.basics.value.Rounding

none
• ### Method Detail

• #### ofDecimalPlaces

public static HalfUpRounding 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.

Specified by:
ofDecimalPlaces in interface Rounding
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

public static HalfUpRounding 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.

Specified by:
ofFractionalDecimalPlaces in interface Rounding
Parameters:
decimalPlaces - the number of decimal places to round to, from 0 to 255 inclusive
fraction - the fraction of the last decimal place, such as 32 for 1/32, from 0 to 256 inclusive
Returns:
the rounding convention
Throws:
IllegalArgumentException - if the decimal places or fraction is invalid
• #### round

public double round(double value)
Description copied from interface: Rounding
Rounds the specified value according to the rules of the convention.
Specified by:
round in interface Rounding
Parameters:
value - the value to be rounded
Returns:
the rounded value
• #### round

public BigDecimal round(BigDecimal value)
Description copied from interface: Rounding
Rounds the specified value according to the rules of the convention.
Specified by:
round in interface Rounding
Parameters:
value - the value to be rounded
Returns:
the rounded value
• #### equals

public boolean equals(Object obj)
Overrides:
equals in class Object
• #### hashCode

public int hashCode()
Overrides:
hashCode in class Object
• #### toString

public String toString()
Overrides:
toString in class Object
• #### meta

public static HalfUpRounding.Meta meta()
The meta-bean for HalfUpRounding.
Returns:
the meta-bean, not null
• #### metaBean

public HalfUpRounding.Meta metaBean()
Specified by:
metaBean in interface Bean
• #### property

public <R> Property<R> property(String propertyName)
Specified by:
property in interface Bean
• #### propertyNames

public Set<String> propertyNames()
Specified by:
propertyNames in interface Bean
• #### getDecimalPlaces

public int getDecimalPlaces()
Gets the number of decimal places to round to.

Rounding follows the normal RoundingMode.HALF_UP convention.

The value must be from 0 to 255 inclusive.

Returns:
the value of the property
• #### getFraction

public int getFraction()
Gets the fraction of the smallest decimal place to round to.

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.

Returns:
the value of the property