Get percentage between 2 numbers - php

so I have a poll website, each question has 2 alternatives. Let's say question A has 130 answers on YES and 90 answers on NO, how can i calculate how many percentage chose the YES answer, and how many people chose the NO answer?
I have tried multiple equations for example but they all end up giving false information. I have been working with this for hours, but math is not really my strongest subject. I'd really appreciate help on this one. Thanks!

here is how you can do this
<?php
$yes = 130;
$no = 90;
$total_votes = $yes+$no;
if($total_votes > 0){
$yes_percentage = ($yes/$total_votes) *100;
$no_percentage = ($no/$total_votes) *100
}
else{
$yes_percentage = 0;
$no_percentage = 0;
}

First, you need to get total count.
$total = $yes + $no;
Then, if you want percentage of yes.
($yes / $total) * 100;

percentage for YES
Yes % = (total answers of yes / total ansers)*100
$yes = 130;
$no = 90;
$totals = $yes+$no;
$yes_per = ($yes/$totals) *100;
percentage for No
No % = (total answers of No / total ansers)*100
$no_per = ($no/$totals) *100

TotalParticipants=130+90=220
YesVotedParticipants=130
NoVotedParticipants=90
YesVotedPaticipants percent(%)= (YesVotedParticipants/TotalParticipants)*100
= (130/220)*100 =59.09 %
NoVotedPaticipants percent(%)= (NoVotedParticipants/TotalParticipants)*100
= (90/220)*100 =40.90 %

Related

How to display number in percentage

I wanted to know how I present a percentage, here is the code
//The number taken from the database
$minos = $ud['bnk']['gold'];
The number that is supposed to be in percent through the database (the database has a number and not a percentage for example 2)
$plus= $ud['bnk']['ent_level'];
And here is a simple calculation of X + 2% = Y
$sava = $minos + $plus;
I tried to do this, according to an internet guide, but it doesn't work for me, I want the number to be a percentage and not successful
function get_percentage($total, $number)
{
if ( $total > 0 ) {
return round($number / ($total / 100),$ud['bnk']['ent_level']);
} else {
return 0;
}
}
$minos = $ud['bnk']['gold'];
$plus = get_percentage(100,$ud['bnk']['ent_level']).'%';
$sava = $minos + $plus;
I solved the equation with what you gave me, thank you very much, the way is:
$minos = $ud['bnk']['gold'];
$plus = ($minos / 100) * $ud['bnk']['ent_level'];
$sava = $plus;
Thanks so much to everyone who helped

Php Calculate percentage difference between two numbers

I am trying to find the difference of two numbers in percentage. The actual scenario is iam getting a total value of user data for this week and for last week. Now i need to see the performance difference in percentage with this weeks data and last weeks data. Following is my code which i am trying. But at times either any of the data will be zero and am getting an error "Division by zero". How to handle that?
$this_week_cust=$row["cust_count_new"];
$last_week_cust=$row["cust_count_old"];
$percentChange = (1 - $last_week_cust / $this_week_cust) * 100;
You can check if $this_week_cust is zero and just set the change to 100%
$this_week_cust=$row["cust_count_new"];
$last_week_cust=$row["cust_count_old"];
if ( $this_week_cust == 0 ) {
$percentChange = -100;
}
else {
$percentChange = (1 - $last_week_cust / $this_week_cust) * 100;
}
Altough the change would be 0 if $last_week_cust was also 0, so perhaps
if ( $this_week_cust == 0 ) {
$percentChange = ($last_week_cust==0)?0:-100;
}
I think this will help you.
$original= 1000;
$current = 5;
$diff = $current - $original;
$more_less = $diff > 0 ? "More" : "Less";
$diff = abs($diff);
$percentChange = ($diff/$original)*100;
echo "$percentChange% $more_less agaist $original";
try this
if $this_week_cust!=0 that time only calculation will perform otherwise not do anything
$this_week_cust=$row["cust_count_new"];
$last_week_cust=$row["cust_count_old"];
if($this_week_cust!=0){
$percentChange = (1 - $last_week_cust / $this_week_cust) * 100;
}
You can get the desired result by checking $this_week_cust, if is it return zero, null or empty set $this_week_cust to 100;
$percentChange=100;

If-else statement to count some variable

I need some help to solved this algorithm problem with How a Person is popular in his city.
My situation
How the algorithm should work like
If a person "mark" has 500 friends in his city out of 500,000.
(500/500,000)*50,000 = 5
So 5 in 50,000 people Know him right.
But When friends count increase the 50,000 should decrease
If "sam" has 1000 friends then
(1000/500,000)*25000 = 5
So 5 in 25000 people know his name
Yes we could implement this in if/else condition
If so then i have to write 500 lines of code.
Is there another way to do this in PHP?
<?php
$totalfriends = 100;
$totali = 5000000;
$name = "Sam";
if ($totalfriends >= 100 && $totalfriends <= 499 ) {
$r = ($totalfriends/$totali)*50000;
echo round($r),' ',"in 50K People on City regonize this Profile";
}else if ($totalfriends >= 500 && $totalfriends <= 999) {
$r = ($totalfriends/$totali)*25000;
echo round($r),' ',"in 25K People on City know".$name;
}else{
echo "";
}
?>
is this what you are looking for?
foreach([100, 500, 543, 1000, 5000, 51000, 500000] as $my_friends)
echo '5 in '. getScoreOf($my_friends) . "<br>";
function getScoreOf($my_friends){
$of = 5;
$total = 5e5; //that's 500,000 ;)
$step = 100; //minimum step, so output is not "4604" but "4600"
$out_of = $total / $my_friends * $of;
return $out_of > $step? round($out_of / $step) * $step: round($out_of);
}
run it in sandbox
edit: solution merged with original code
<?php
$of = 5;
$totalfriends = 100;
$name = "Sam";
echo $of ." in ". getScoreOf($of, $totalfriends) ." people in city know ". $name;
function getScoreOf($of, $my_friends){
$total = 5e6; //that's 5,000,000 ;)
$step = 100; //minimum step, so output is not "4604" but "4600"
$out_of = $total / $my_friends * $of;
return $out_of > $step? round($out_of / $step) * $step: round($out_of);
}

Vote up down total max

Hey tried to search for similar but guess my english fail me lol, well here is what i need help with, im trying to make a vote system with up/down vote and wanna show it like this
5.3/10
but have no idea how to make total "5.3" not go over 100% = 10 here is my code so far
<?php
$Vote_up = 804;
$Vote_down = 942;
$total = $Vote_up + $Vote_down;
$result = 100;
echo number_format($total/$result,1,",",".") . "/10";
?>
result is 17,5/10
ps. new to php so be easy on me ^^
i am really bad at maths but i think this should be work
$Vote_up = 555;
$Vote_down = 555;
$total = $Vote_up + $Vote_down;
if( $total <= 0 ){
$score = -11;
} else {
$score = (($Vote_up / $total) + ($Vote_down / $total) * 10) * -1;
}
echo floor( $score + 11 ) . '/10';
if someone have a better solution, please i would like to know it - thanks!

Adding Percentage of Number to Itself

I want to add 30.4 percent of $drivervalue to $drivervalue, is it possible to do that as below? I know if I knew what the actual number was going to be I could just put the 30.4 after the 1., but as I wont know what it will be, I am using the variable instead is this a valid syntax?
//example
$drivervalue = 16000000;
$percentdiff = 30.4;
$drivvalue *= 1. $percentdiff;
$num = 100;
$percentage = 30.4;
$num += $num*($percentage/100); // results in 130.4
$drivervalue *= (1 + $percentdiff / 100);
$drivervalue = $drivervalue * (1 + ($percentdiff / 100));

Categories