Package com.opengamma.strata.collect
Class Percentage
- java.lang.Object
-
- com.opengamma.strata.collect.Percentage
-
public final class Percentage extends Object
A percentage amount, with a maximum of 10 decimal places.A number has three standard representations in finance:
- decimal form - the form needed for mathematical calculations, as used by most parts of Strata
- percentage - where 1.2% is the same as the decimal 0.012
- basis points - where 120bps is the same as the decimal 0.012
-
-
Field Summary
Fields Modifier and Type Field Description static PercentageZEROA percentage of zero.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)Checks if this instance equals another.static PercentagefromBasisPoints(BasisPoints basisPoints)Obtains an instance from a basis points value, where 70bps will create an instance representing 0.7%.static PercentagefromDecimalForm(double decimal)Obtains an instance from mathematical decimal form, where 0.007 will create an instance representing 0.7%.static PercentagefromDecimalForm(Decimal decimal)Obtains an instance from mathematical decimal form, where 0.007 will create an instance representing 0.7%.inthashCode()Returns a suitable hash code.Percentagemap(UnaryOperator<Decimal> mapper)Applies an operation to the value.Percentageminus(Percentage other)Returns a percentage equal to the this percentage minus the other one.static Percentageof(double percentage)Obtains an instance from a percentage value.static Percentageof(Decimal percentage)Obtains an instance from a percentage value.static Percentageparse(String str)Parses a percentage.Percentageplus(Percentage other)Returns a percentage equal to the this percentage plus the other one.BasisPointstoBasisPoints()Converts this percentage to the equivalent basis points.DecimaltoDecimalForm()Converts this percentage to mathematical decimal form.StringtoString()Returns the formal string representation, '{value}%'.DecimalvaluePercent()Gets the value in percentage form as aDecimal.
-
-
-
Field Detail
-
ZERO
public static final Percentage ZERO
A percentage of zero.
-
-
Method Detail
-
of
public static Percentage of(double percentage)
Obtains an instance from a percentage value.- Parameters:
percentage- the percentage value- Returns:
- the percentage object, rounded (HALF_UP) to 10 decimal places
-
of
public static Percentage of(Decimal percentage)
Obtains an instance from a percentage value.- Parameters:
percentage- the percentage value- Returns:
- the percentage object, rounded (HALF_UP) to 10 decimal places
-
fromDecimalForm
public static Percentage fromDecimalForm(double decimal)
Obtains an instance from mathematical decimal form, where 0.007 will create an instance representing 0.7%.- Parameters:
decimal- the mathematical decimal value- Returns:
- the percentage object, rounded (HALF_UP) to 10 decimal places
-
fromDecimalForm
public static Percentage fromDecimalForm(Decimal decimal)
Obtains an instance from mathematical decimal form, where 0.007 will create an instance representing 0.7%.- Parameters:
decimal- the mathematical decimal value- Returns:
- the percentage object, rounded (HALF_UP) to 10 decimal places
-
fromBasisPoints
public static Percentage fromBasisPoints(BasisPoints basisPoints)
Obtains an instance from a basis points value, where 70bps will create an instance representing 0.7%.- Parameters:
basisPoints- the basis points value- Returns:
- the percentage object
-
parse
public static Percentage parse(String str)
Parses a percentage.The percentage may be suffixed by '%' or 'pct'.
- Parameters:
str- the percentage string- Returns:
- the percentage object, rounded to 10 decimal places
-
valuePercent
public Decimal valuePercent()
Gets the value in percentage form as aDecimal.A value of 1.5% will return 1.5.
- Returns:
- the value in percentage form, with a maximum of 10 decimal places
-
toDecimalForm
public Decimal toDecimalForm()
Converts this percentage to mathematical decimal form.A value of 1.5% will return 0.015.
- Returns:
- the amount in mathematical decimal form
-
toBasisPoints
public BasisPoints toBasisPoints()
Converts this percentage to the equivalent basis points.A value of 1.5% will return 150bps.
- Returns:
- the amount in basis points form
-
plus
public Percentage plus(Percentage other)
Returns a percentage equal to the this percentage plus the other one.- Parameters:
other- the other percentage- Returns:
- the resulting percentage
-
minus
public Percentage minus(Percentage other)
Returns a percentage equal to the this percentage minus the other one.- Parameters:
other- the other percentage- Returns:
- the resulting percentage
-
map
public Percentage map(UnaryOperator<Decimal> mapper)
Applies an operation to the value.This is generally used to apply a mathematical operation to the value. For example, the operator could multiply the value by a constant, or take the inverse.
abs = base.map(value -> value.abs());
- Parameters:
mapper- the operator to be applied to the amount- Returns:
- a copy of this value with the mapping applied to the original amount
-
equals
public boolean equals(Object obj)
Checks if this instance equals another.
-
hashCode
public int hashCode()
Returns a suitable hash code.
-
-