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 Quote Link to comment Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 $discountPrice = ($totalPrice * .05); Quote Link to comment 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; ?> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.