SilverMarc
Mar 9 2009, 08:11 PM
Simple for any brainy PHP person (which, you can see, isn't me...)
I have a number variable $small
And another number variable $big
I want to know what $percent the $small is to the $big, rounded off to the nearest 10th
Examples:
$small = 50
$big = 100
$percent = 50
or
$small = 74
$big = 100
$percent = 70
Easy to use php would be great - this must be simple for most of you!
Thanks.
Brian Chandler
Mar 10 2009, 12:17 AM
Basic trick for rounding: use floor(), which rounds down to a whole number.
So to round to multiples of 10, divide by 10, use floor(), then multiply by 10 again.
In this case you need to multiply by 100 to get a percentage, so use x10 then floor() then x10:
$percentageintens = 10*floor(10*$a/$b);
---
As the Donald Knuth quote says: "Beware of bugs in the above. I have only proved it correct -- I have not tested it."