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
https://forums.phpfreaks.com/topic/43491-solved-calculate-percentage-discount/
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; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.