Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.