Package com.opengamma.strata.collect
Class FixedScaleDecimal
- java.lang.Object
-
- com.opengamma.strata.collect.FixedScaleDecimal
-
- All Implemented Interfaces:
Serializable
,Comparable<FixedScaleDecimal>
public final class FixedScaleDecimal extends Object implements Serializable, Comparable<FixedScaleDecimal>
A decimal number based onDecimal
with a fixed scale.This is a lightweight wrapper for
Decimal
that ensures the scale is fixed, between 0 and 18. It is most useful for monetary values.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(FixedScaleDecimal other)
Decimal
decimal()
Gets the underlying decimal.boolean
equals(Object obj)
int
fixedScale()
Gets the fixed scale.int
hashCode()
FixedScaleDecimal
map(UnaryOperator<Decimal> fn)
Maps this value using the maths operations ofDecimal
.static FixedScaleDecimal
of(Decimal decimal, int fixedScale)
Obtains an instance from a decimal and scale.static FixedScaleDecimal
parse(String str)
Parses an instance from aString
.BigDecimal
toBigDecimal()
Gets the value as aBigDecimal
with the fixed scale.String
toString()
Returns the formal string representation of the fixed scale decimal.
-
-
-
Method Detail
-
of
public static FixedScaleDecimal of(Decimal decimal, int fixedScale)
Obtains an instance from a decimal and scale.- Parameters:
decimal
- the underlying decimalfixedScale
- the fixed scale, equal or greater than the scale of the decimal- Returns:
- the equivalent decimal
-
parse
public static FixedScaleDecimal parse(String str)
Parses an instance from aString
.- Parameters:
str
- the string- Returns:
- the equivalent decimal
- Throws:
NumberFormatException
- if the string cannot be parsedIllegalArgumentException
- if the value is too large
-
decimal
public Decimal decimal()
Gets the underlying decimal.The decimal may have a smaller scale, but it will not have a larger scale.
- Returns:
- the decimal
-
fixedScale
public int fixedScale()
Gets the fixed scale.- Returns:
- the fixed scale, from 0 to 18
-
map
public FixedScaleDecimal map(UnaryOperator<Decimal> fn)
Maps this value using the maths operations ofDecimal
.The result must have a scale equal or less than the fixed scale.
- Parameters:
fn
- the function to apply- Returns:
- the result of the function
- Throws:
IllegalArgumentException
- if the result is too large
-
toBigDecimal
public BigDecimal toBigDecimal()
Gets the value as aBigDecimal
with the fixed scale.- Returns:
- the decimal, with a scale equal to the fixed scale
-
compareTo
public int compareTo(FixedScaleDecimal other)
- Specified by:
compareTo
in interfaceComparable<FixedScaleDecimal>
-
-