Re: PHP 4.0 Bug #7429: percent(); whay not to make it:
| From: | Ignacio Vazquez-Abrams | Date: | Wed, 25 Oct 2000 21:50:23 +0000 |
| Subject: | Re: PHP 4.0 Bug #7429: percent(); whay not to make it: | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-36015@lists.php.net to get a copy of this message | ||
Interesting idea. Some thoughts though:
1) Why not have more than two values?
2) Why pass which one should be used instead of just using the first one?
3) Maybe the function isn't the right place to do rounding.
With those in mind, here is a modified version (for PHP 4):
function percent()
{
for($tally=0, $i=0; $i<func_num_args(); ++$i)
$tally+=func_get_arg($i);
return ($tally?(func_get_arg(0)*100/$tally):0);
};
On 24 Oct 2000 maxim@maxim.cx wrote:
> 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
>
>
>
--
Ignacio Vazquez-Abrams
Vice President
OPENSERVICES CORPORATION
TEL# +1.416.632.7818
FAX# +1.800.687.6133