Here is the problem
I have 200 votes and 53 of them must be calculated as the porcent so I run this example to test the math first
$total="200";
$votes="53";
$calculate=($total / 100) * $votes;
echo "The porcent of votes is ".$calculate." ";
The result must be something like 26% but I get
Result 53% ...
What can be wrong ?
Here:
<?php
$total="200";
$votes="53";
$calculate=($votes * 100) / $total;
echo "The porcent of votes is ".$calculate."%";
Your calculate formula is wrong. If you want to calculate the percent of any number, you need the given or scored number and the total number. In your case, given is or score is 53 and total it 200. So you divide given or score by total and the multiply the results by 100 to get the percent. See below.
$total="200";
$votes="53";
$calculate=($votes / $total) * 100;
echo "The porcent of votes is ".$calculate." ";
Let me know if this helps. Stay warm!
Related
I am trying to figure out if there is a way to find the percentage between 2 numbers.
It's a progress / ranking system.
I want to find the percentage the $current_exp is between the $current_min and the $current_max values, is there a way to achieve this in PHP? So far I've got to this, but it doesn't work as you progress in ranks, it doesn't treat the $current_min as 0 so when you rank up, it says you are like 75% into your next rank progression when you're in fact 0. Does this make sense?
$currentProg = ($current_exp * 100) / $current_max;
Say the current minimum is 18750 and the current maximum is 25100, the current exp is 22000... What percentage from the min to the max is the current exp? This will change each rank as the $current_min and $current_max variables get set depending on the exp of the user.
The next rank is Current min is 25100 Current max is 34230
Currently, when you are at 26000 exp, the output is saying 75.956763073327% which is not correct, it should be like 1 or 2%?
Thanks in advance 🙏
Not a good mathematician, but it looks like it should be:
(Difference of rank - minimum) / (Difference of maximum - minimum) * 100
<?php
$x = 25100;
$z = 34230;
$y = 26000;
echo ($y - $x + 1) / ($z - $x + 1) * 100; // outputs 9.8674843938232 %
Online Demo
Note: + 1 is added to both numerator and denominator to avoid divide by zero errors.
I try to calculate growth but if I have like last month value zero then it gives me:
'NaN'
All these values gives me 'NaN':
Examples what can be my values:
var_dump((($this_month - $last_month) / $last_month) * 100);
var_dump(((0 - 0) / 0) * 100);
var_dump(((5 - 0) / 0) * 100); //this should be 100% not 'NaN'
Doing something wrong or my math calculation is wrong?
I used this first accepted answer (The percentage increase ...):
https://stackoverflow.com/questions/5799055/calculate-percentage-saved-between-two-numbers
I have negative percentages also if last month was successful and current month isnt successful.
x/0 is undefined, no matter what x is.
If your $last_month == 0, you're dividing by zero, which mathematically is undefined.
NaN stands for "Not a Number", i.e. undefined.
The response your code is giving you is correct.
I have good reviews, and I have bad reviews. I need calculate rating from this reviews for post.
Example:
Post 1 have 1 good reviews, and 2 bad reviews
Post 2 have 12 good reviews, and 5 bad reviews
Post 3 have 0 good reviews, and 0 bad reviews
How I can calculate rating? I need for post get 5 stars. I need score up to 5 stars or less. May be I need this formula?
$score = ($good_reviews * $bad_reviews) / 5; //get rating stars
But I don't get 5, or less number. How I can do it correctly?
Maybe you want this:
$rating = $good_reviews
? intval ( 5.4 * $good_reviews / ( $good_reviews + $bad_reviews))
: 0;
And now step by step:
SUM := $good_reviews + $bad_reviews is the sum of all reviews.
RATE := $good_reviews / SUM is the general rating of $good_reviews to the sum of reviews. The result is in the range 0.000 to 1.000 .
Multiplying it with 5.4 expands the range to 0.000 to 5.400. Its a little bit tricky to allow some bad votes for a 5 stars ranking. The factor can be every number between 5.000 and 5.9999999.
intval() reduces the number to an int from 0 to 5 (your stars).
The alternative by ?: avoids an division by zero error.
That formula will not give you what you need. It will only multiply good with bad and divide by 5. For example ((100 good * 20 bad) / 5) = 400. Way out of 5!
If you need score up to five stars you will need to use ranges.
Calculate percentage between good and bad and then do an if checks.
For example:
$percentage = (($good - $bad) / $good) * 100;
if($percentage => 100) {
//5 starts
} else if ($percentage < 100 && $percentage => 80) {
//4 stars
} else if ($percentage < 80 && $percentage => 60) {
//3 stars
} else if ($pecentage < 60 && $percentage => 40) {
//2 starts
} else {
//1 star
}
That's just a basic example. There are different ways to approach this. You need to adjust it to your needs and try if it works for you.
I did this really quick, so didn't test it. Please, check and see if it fits you. I just wanted to give you an idea.
i have database list for users and user's coupons and i want to add +1 ticket per coupon if bigger than two between numbers.
For example:
100 between 200 = 1 ticket
200 between 300 = 2 ticket
300 between 400 = 3 ticket
.......
.......
1200 between 1300 = 12 ticket
i put photo for example:
My Code is :
$q=$db->query("SELECT DISTINCT client_id FROM kuponlar ORDER BY client_id LIMIT 20");
foreach($q as $cat){
echo '<li id="'.$cat['client_id'].'" class="files">';
echo 'User ID: '.$cat['client_id'].'';
echo '<ul class="sub-menu">';
$linkq=$db->query("SELECT * FROM kuponlar WHERE client_id='" . $cat['client_id'] . "'");
foreach($linkq as $link){
echo '<li>Coupon ID: '.$link['kuponid'].' - Coupon Price: '.$link['yatirimi'].' â‚ş / Won Ticket: '.substr($link['yatirimi'], 0, 1).' </li>';
}
echo '</ul></li>';
}
As discussed in the comments;
So if the number is 12 000, you want 120 as a result? Basically divide by 100? – Qirel
Yes #Qirel this is perfect comment. i mean like this. (...) – Ismail Altunören
So put simply, you want to divide the number by 100. You must then floor it, to get a full integer and get rid of any decimal points.
floor($link['yatirimi']/100);
You would replace that with your substr(), making the full line
echo '<li>Coupon ID: '.$link['kuponid'].' - Coupon Price: '.$link['yatirimi'].' â‚ş / Won Ticket: '.floor($link['yatirimi']/100).' </li>';
floor() docs
You can do this using floor():
http://php.net/manual/en/function.floor.php
floor — Round fractions down
So this should do the trick: floor($link['yatirimi'] / 100)
Use that in place of your substr.
you can user this: $wonTicketsCount = round(($link['yatirimi'] / 100 ) - 0.5); instead substr($link['yatirimi'], 0, 1).
Refering to your comments if you only use hundreds steps :
Modify this line :
substr($link['yatirimi'], 0, 1)
This line will always take the first number.
By : this one
This will take all numbers excepts the two lasts.
substr($link['yatirimi'],0,-2);
By keeping the substr, it will not work for number between 0-100,
It's better to use #Qirel's Anwser.
I need a calculation to finish, since I already have the calculation to give the appropriate level according to EXP, now I need to remove this EXP, example:
If the user has 1050 of EXP, the calculation must remove all values equal to 100, which means that he withdraws the 1000 and leaves the 50.
My code is:
$limit_exp = 100;
$player_exp = selectplayerexp($db);
//this variable calculate the according lvl, ex: 1000/100 = lvl 10
$levelup = (int) ($player_exp / $limit_exp);
//this one have to withdraw exp equal to 100, ex 1150, stay with 50exp
$withdraw = (int) ( ?????????????? );
Please, What should I do?
You can use the modulo (%) operator for this. This will give you the remainder of a division.
If you want to find the amount remaining, it's:
$player_exp % $limit_exp
It looks like you're trying to find an amount to subtract from what you have stored in the database, so you can subtract that amount from the total.
$withdraw = $player_exp - $player_exp % $limit_exp;
There should be no need for an (int) cast on that calculation, as the % expression will evaluate to an integer.