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 onDecimalwith a fixed scale.This is a lightweight wrapper for
Decimalthat 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 intcompareTo(FixedScaleDecimal other)Decimaldecimal()Gets the underlying decimal.booleanequals(Object obj)intfixedScale()Gets the fixed scale.inthashCode()FixedScaleDecimalmap(UnaryOperator<Decimal> fn)Maps this value using the maths operations ofDecimal.static FixedScaleDecimalof(Decimal decimal, int fixedScale)Obtains an instance from a decimal and scale.static FixedScaleDecimalparse(String str)Parses an instance from aString.BigDecimaltoBigDecimal()Gets the value as aBigDecimalwith the fixed scale.StringtoString()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 aBigDecimalwith the fixed scale.- Returns:
- the decimal, with a scale equal to the fixed scale
-
compareTo
public int compareTo(FixedScaleDecimal other)
- Specified by:
compareToin interfaceComparable<FixedScaleDecimal>
-
-