PHP 4.0 Bug #7429: percent(); whay not to make it:
| From: | maxim at maxim dot cx | Date: | Tue, 24 Oct 2000 12:08:21 +0000 |
| Subject: | PHP 4.0 Bug #7429: percent(); whay not to make it: | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-35984@lists.php.net to get a copy of this message | ||
From: maxim@maxim.cx
Operating system: RED HAT6.2 Linux
PHP version: 4.0.2
PHP Bug Type: Feature/Change Request
Bug description: percent(); whay not to make it:
Hello Developers,
While making a simple class that would calculate few statistics about my visitors I came up with
this:
function percent($num1, $num2, $int, $dec=0)
{
$sum = $num1+$num2;
if($sum==0) return 0;
return round(${"num$int"}*100/$sum+0.00000001,$dec);
}
This is a very simple function to calculate percentage of two numbers.
an Example of use:
# In a room there are:
$males = 42;
$females = 67;
# To calculate what percent of the males is currently in the room the call to the function would
be:
$male_percent = percent($males, $females, 1);
# Female's percentage?
$female_percent = percent($males, $females, 2);
# We could of course do 100-$male_percent to know $female_percent
# but if this is a class, loop or something then switch $int to 2 would often be much simpler,
though.
The option to select the number is very useful for me when the data rotates in different orders and
the numbers to base on need to be switched.
An optional $dec is the [int] of round() to pass.
And since I like round to be more precise there is +0.00000001.
I am sure that percent() could be made simpler by adding some more portability to it.
For example it would be useful optionally not to require both numbers passing to percent() only the
$sum and a number right away. But once again it is nice (at least for me) to be able to switch
between numbers.
Please forgive me if this idea was up there long before me and/or if this function is completely
useless.
Just though that it would be nice to have one like this. (PHP doesn't have one yet, right?)
Maxim Maletsky
maxim@maxim.cx
--
Edit Bug report at: http://bugs.php.net/?id=7429&edit=1