@sensimods/utility-lab
    Preparing search index...

    Function calculateDiscountPercentage

    • Calculates the discount percentage between an original price and a new price. Safely handles division by zero and rounds to two decimal places to prevent long floats.

      Parameters

      • originalPrice: number

        The starting price (must be greater than 0).

      • newPrice: number

        The discounted price.

      Returns number

      The discount percentage, rounded to a maximum of 2 decimal places.

      calculateDiscountPercentage(100, 80) // 20
      calculateDiscountPercentage(29.99, 25) // 16.64

      Will throw an error if the original price is 0 or less.