Removing a discount is not as simple as adding the discounted amount back in if the logic for applying the discount applies it on the new number. If a post-discount operation can be performed then the math is much easier (add x * discount
to the discounted price.
A discount applied to the original number is slightly tricker. For the math below, discount
is a percentage and cost
will be a dollar figure.
discount = 10%
cost = $100
A discounted cost can be calculated discount
to cost
:
discounted_cost = cost - (cost * discount) => $90
Simply applying an addition of discount
does not work, it results in a new cost that’s slightly less than the expected (original) cost:
new_cost = cost + (cost * discount) => $110
discounted_cost = new_cost - (new_cost * discount) => $99
A formula with one unknown increase
used to balance discount
against the original cost
can be written as:
(cost + cost * increase) - ((cost + cost * increase) * discount) = cost
Simplifying that formula:
(x + (x * z)) - ((x + x * z) * y) = x
z => y/(-y + 1)
Now a formula increase
given discount
can be written:
increase = discount / (-discount + 1) => 0.1111
And a formula for new_cost
given only discount
:
new_cost = cost + cost * (discount / (-discount + 1)) => $111.1111
Calculating the new total and proving it balances with the original cost:
new_total = new_cost - (new_cost * discount) => $100
A simple function for Python could be written as follows:
def remove_discount(cost, discount):
return cost + cost * (discount / (-1 * discount + 1))