Jump to content

[SOLVED] Getting a percentage


timmah1

Recommended Posts

ok, i've been at this for two days now, and it's not working.

 

I'm trying to find a percentage of 3 numbers.

 

Here's an example of what I am doing

 

$total = 2+3;
$total_overall = 6;

$group1 = $total * $total_overall;
$group2 = $group1 % 100;
$groups = number_format($group2, 0);

 

If $total_overall is equal to $total, it should be 100%

If $total_overall is less than $total, it should give me a percentage under 100%

If $total_overall is greater than $total, it should stay at 100%

 

For some reason, if $total_overall is more than $total, it gives me a percentage under 100%.

 

Can anybody help me out here?

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/
Share on other sites

thanks for the reply rarebit, but I have to be honest, that makes no sense to me

 

What doesn't make sense to you?

 

 

The basic percent formula is (max/number)*100 (technically grouping isn't needed there) like someone already said.

 

 

Or, do you mean what is modulus?

 

5 mod 2 for example is 1, since the remainder of 5/2 is 1.

 

 

Modulus simply takes the 'remainder'....

 

20 % 5 = 0

20 % 6 = 2

20 % 11 = 9

 

So on....

 

100%20 = 0

100%21 = 19

jabop,

 

That works perfect except for one thing.

If overall total is higher than 100, it goes more than 100

 

$total = 3+2;
$over_total = 6;

echo (($total / $total_overall) * 100) . "%";

It shows 101%

 

How do I limit it to go no higher than 100?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.