can't figure out how to correct a rounding issue

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

Post Reply
npd1164
Forum Newbie
Posts: 1
Joined: Wed Dec 02, 2009 8:20 pm

can't figure out how to correct a rounding issue

Post by npd1164 »

Trying to pull out the percentage of a discounted price. Anyone have any ideas as to how to get correct amount in both situations? :banghead:

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

Re: can't figure out how to correct a rounding issue

Post by cpetercarter »

What's your problem? The difference between .28 and .22 is .06, which is 21% of .28 (to the nearest whole number).
Post Reply