Jump to content

[SOLVED] Calculate Percentage Discount


stevesimo

Recommended Posts

Hi, I have a field which holds a numerical value lets say 29.99

 

Based on whatever the value of this number is I then want to calculate 5% of the value

 

Do I just do $discountPrice = $totalPrice * 5%

 

Im not sure what syntax to use to achieve this

 

Thanks

 

Steve

Link to comment
Share on other sites

This should do the trick

$discountPrice = ($totalPrice - ($totalPrice * 0.05))

 

Here is a little test to see.

 

<?php 
$totalPrice = 100;
$discount = ($totalPrice * 0.05);
$discountPrice = ($totalPrice - ($totalPrice * 0.05)); 
echo $totalPrice; 
echo '<br><br>'.$discount; 
echo '<br><br>'. $discountPrice; ?>

 

I might even recommend doing some rounding and do it like this, since dealing with money values.

 

 <?php 
$totalPrice = 100;
$discount = round(($totalPrice * 0.05),2);
$discountPrice = ($totalPrice - $discount); 
echo $totalPrice; 
echo '<br><br>'.$discount; 
echo '<br><br>'. $discountPrice; ?>

Link to comment
Share on other sites

$discountPrice = ($totalPrice * .05);

 

 

that will only show how much is discounted not what the new price is after the discount, you need to subtract the discount amount from the initial price to get the discounted price.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.