stevesimo Posted March 20, 2007 Share Posted March 20, 2007 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 More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 $discountPrice = ($totalPrice * .05); Link to comment https://forums.phpfreaks.com/topic/43491-solved-calculate-percentage-discount/#findComment-211204 Share on other sites More sharing options...
grimmier Posted March 20, 2007 Share Posted March 20, 2007 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 https://forums.phpfreaks.com/topic/43491-solved-calculate-percentage-discount/#findComment-211208 Share on other sites More sharing options...
grimmier Posted March 20, 2007 Share Posted March 20, 2007 $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 https://forums.phpfreaks.com/topic/43491-solved-calculate-percentage-discount/#findComment-211209 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 Obviously, as you wrote out a whole segment, I simply answered the question being asked, what is the correct syntax. Obviously the % is not correct syntax. I bet he is grateful that you went above and beyond. Link to comment https://forums.phpfreaks.com/topic/43491-solved-calculate-percentage-discount/#findComment-211251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.