PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
npd1164
Forum Newbie
Posts: 1 Joined: Wed Dec 02, 2009 8:20 pm
Post
by npd1164 » Wed Dec 02, 2009 8:30 pm
Trying to pull out the percentage of a discounted price. Anyone have any ideas as to how to get correct amount in both situations?
Code: Select all
$total = 1.00; // original price
$disc = .80; //price after applying a 20% discount
echo number_format(100-(($disc / $total)*100), 0) . '%'; //results in correct 20%
$total = .28;
$disc = .22;
echo number_format(100-(($disc / $total)*100), 0) . '%'; //unwanted results - shows up as 21%
cpetercarter
Forum Contributor
Posts: 474 Joined: Sat Jul 25, 2009 2:00 am
Post
by cpetercarter » Thu Dec 03, 2009 1:34 am
What's your problem? The difference between .28 and .22 is .06, which is 21% of .28 (to the nearest whole number).