Page 1 of 1

can't figure out how to correct a rounding issue

Posted: Wed Dec 02, 2009 8:30 pm
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%

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

Posted: Thu Dec 03, 2009 1:34 am
by cpetercarter
What's your problem? The difference between .28 and .22 is .06, which is 21% of .28 (to the nearest whole number).