Reputation: 334
Not to sound stupid but I was wondering if there is a very simple way of taking a percentage from a number in PHP?
Say I have a number of 70.40 and I want to remove 15% of this.
So far I have something like:
$discount = 15;
$price = 70.40;
$newPrice = $price - (($discount / 100) * $price);
But this seems to be giving me all kinds of random numbers.
is there an easier way?
Upvotes: 0
Views: 166
Reputation: 945
Try this:
function apply_discount($price, $discount) {
$after_discount = $price - (($price / 100) * $discount);
return $after_discount;
}
e.g.
echo apply_discount(200, 15); //170
Upvotes: 2
Reputation: 133
70.40 * 0.15 = 10.56
whereas 0.15 is 15%
I guess you could do something like this.
$discount = 0.15;
$price = 70.40;
$newPrice = $price - ($price * $discount);
Upvotes: 0
Reputation: 509
$discount = 15;
$price = 70.40;
$newPrice = $price * (100 - $discount) / 100;
If your $discount
would be a percentage (between 0 and 1) it would be:
$discount = 0.15;
$price = 70.40;
$newPrice = $price * (1 - $discount);
Upvotes: 2