Help - Search - Members - Calendar
Full Version: PHP Percentage
HTMLHelp Forums > Programming > Server-side Scripting
SilverMarc
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
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."
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.