Class 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
    This class allows the use of percentage form to be explicit.
    • 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 a Decimal.

        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.
        Overrides:
        equals in class Object
        Parameters:
        obj - the other instance
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Returns a suitable hash code.
        Overrides:
        hashCode in class Object
        Returns:
        the hash code
      • toString

        public String toString()
        Returns the formal string representation, '{value}%'.
        Overrides:
        toString in class Object
        Returns:
        the formal string