Calculate percentage of up votes - php

I have searched this site and Google and even though the idea is pretty simple I can't figure it out.
I need to (like seen on YouTube) calculate the % of up-votes based on the amount up-votes and down-votes.
I have two vars, $upvotes and $downvotes now i need to calculate $ratio
For example
$upvotes = 3;
$downvotes = 1;
The ratio here needs to be 75 (%)
If you have
$upvotes = 0;
$downvotes = 100;
It needs to be 0 (%)
How do I calculate the percentage (in PHP)?

Simply
if(($upvotes+$downvotes) != 0)
$percentage = (float)($upvotes/($upvotes+$downvotes))*100;
else
$percentage = 0;

Simple maths!
$ratio = $upvotes / ($upvotes + $downvotes) * 100;

if($downvotes > 0 || $upvotes >0) {
$percentage = ($upvotes / ($upvotes+$downvotes));
}
elseif($upvotes > 0 && downvotes == 0) {
$percentage = 1;
}
$percentage = round(100*$percentage);
$percentage .= "%"; // if you want to add %
I tested it and it works.

$percent = ( $upvotes / ( $upvotes + $downvotes ) ) * 100;

$percentage = (float) round((100 /($upvotes + $downvotes)) * $upvotes, 2);

I would do a round on it aswell, this will round the result to the closest integer.
eg. 75.5% = 76%;
$ratio = round((($upvotes/($upvotes+$downvotes))*100), 0, PHP_ROUND_HALF_UP).'%';

Related

How to calculate total cost based on price ranges of items

I need to calculate total cost of items, based on ranges:
if ($itemCount <11)
{
$cost_for_range = 1;
}
if ($itemCount <26 && $itemCount >=11)
{
$cost_for_range = 0.75;
}
if ($itemCount <50 && $itemCount >=26)
{
$cost_for_range = 0.55;
}
Thing is - I need to count final cost taking earlier levels into consideration - eg if the cart has 30 items, the price would be:
first 10 at $1 = $10
another 15 at $0.75 = $11.25
another 5 at $0.55 = $2.75
so total would be $24.
How to calculate this in php?
Try this :
$itemCount = 25;
$total = 0;
for ( $i = 0 ; $i < $itemCount ; $i++ ) {
if ( $i < 10 ) {
$total += 1;
} else if ( $i < 25 ) {
$total += 0.75;
} else if ( $i < 50 ) {
$total += 0.55;
} else {
$total += 0; // price if more than 50
}
}
Having some fun using min and max to do the various ranges...
function calculateCost ( int $count, float $cost ) {
$total = min(10, $count) * $cost;
$total += (min(15, max(0, $count - 10)) * ($cost * 0.75));
$total += (max(0, $count - 25) * ($cost * 0.55));
return $total;
}
the second calculation uses max(0, $count - 10), so this takes the first 10 off, but ensures it doesn't go negative. Then it uses the min(15, max(0, $count - 10)) to take the lowest of 15 or the number left (so with 30, this will be 15).
The last one is just the max of the count - 25 or 0, so for 30, this is 5.
Use it with...
echo calculateCOst( 30, 1 );
gives 24
For a universal solution, it is good to define the discounts in an array. This solution is then ready for extensions and changes.
$rebate = [10 => 1.0, 15 => 0.75, PHP_INT_MAX => 0.55];
The number and the discount array are transferred to the function that calculates the cost factor. The calculation itself is realized with a simple forech loop with termination condition.
function calcCostFactor(int $count, array $rebate){
$factor = 0.0;
foreach($rebate as $number => $val){
$factor += min($count,$number) * $val;
$count -= $number;
if($count <= 0) break;
}
return $factor;
}
example:
$factor = calcCostFactor(30, $rebate);
Try it self.

Incorrect saturation calculation in RGB to HSL function

Does anybody know the correct formula to get the saturation level from an RGB color?
I already have a function that does it. I've tried loads of them posted on the internet but only this one seemed to work (first-time) for me, apart from the saturation level being occasionally slightly out.
rgb(204,153,51) should equal hsl(40,60,50), instead I have hsl(40,75,50). As you can see my hue and lightness are correct, in-fact, the saturation is mostly correct too, but sometimes it's not and I need to correct that if I can.
This is what I've built so far, so I can check all the color values are correct for my images, before storing them in a database for my search engine.
And this is the function in question where I believe the saturation is being calculated incorrectly:
function RGBtoHSL($red, $green, $blue)
{
$r = $red / 255.0;
$g = $green / 255.0;
$b = $blue / 255.0;
$H = 0;
$S = 0;
$V = 0;
$min = min($r,$g,$b);
$max = max($r,$g,$b);
$delta = ($max - $min);
$L = ($max + $min) / 2.0;
if($delta == 0) {
$H = 0;
$S = 0;
} else {
$S = $delta / $max;
$dR = ((($max - $r) / 6) + ($delta / 2)) / $delta;
$dG = ((($max - $g) / 6) + ($delta / 2)) / $delta;
$dB = ((($max - $b) / 6) + ($delta / 2)) / $delta;
if ($r == $max)
$H = $dB - $dG;
else if($g == $max)
$H = (1/3) + $dR - $dB;
else
$H = (2/3) + $dG - $dR;
if ($H < 0)
$H += 1;
if ($H > 1)
$H -= 1;
}
$HSL = ($H*360).', '.($S*100).', '.round(($L*100),0);
return $HSL;
}
One clue I have, as to why this doesn't work 100%, is that I'm first converting a HEX color to an RGB, then the RGB to an HSL. Would this be a problem due to web-safe colors or can you spot anything else that may cause this in the function? Or is this just how it is?
UPDATE 1
Trying other images, it appears to be mostly 'beige' (approx.) colors that are slightly out on saturation. Using a color picker, if I move the saturation bar to where it should be there isn't a huge difference, so maybe my search feature won't pick up on this too much. It would be nice to solve it though, before I run it on 500,000 photos.
THE FIX
Thanks to OmnipotentEntity, below, he noticed I missing piece to my function. I changed:
$S = $delta / $max;
to:
$S = $L > 0.5 ? $delta / (2 - $max - $min) : $delta / ($max + $min);
and now produces 100% correct results.
FRIENDLY NOTE
If anybody would like the code to produce this color table, just ask.
It looks like you’re missing a piece of the calculation for saturation if your luma is > .5 as shown here in this JavaScript HSL code.
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;

Algorithm to portion

I have a number X, consider X = 1000
And I want piecemeal this number at three times, then Y = 3, then X = (X / 3)
This will give me equal, just not always accurate, so I need: a percentage value is set, also consider K = 8, K is the percentage, but what I want to do? I want the first portion has a value over 8% in K, suppose that 8% are: 500 and the other two plots are 250, 250
The algorithm is basically what I need it, add a percentage value for the first installment and the other equals
EDIT
I just realized, this is far simpler than I made it. To find the value of $div in my original answer you can just:
$div = (int)($num / ($parcels + $percent / 100));
Then the $final_parcels will be the same as below. Basically, the line above replaces the while loop entirely. Don't know what I was thinking.
/EDIT
I think this will do what you want... unless I am missing something.
<?php
$num = 1000;
$percent = 8;
$parcels = 3;
$total = PHP_INT_MAX;
$div = (int)($num / $parcels);
while ($total > $num) {
$div -= 1;
$total = (int)($div * ($parcels + $percent / 100));
}
$final_parcels = array();
$final_parcels[] = ($num - (($parcels - 1) * $div));
for ($i = 1; $i < $parcels; $i++) {
$final_parcels[] = $div;
}
print_r($final_parcels);
This output will be
Array
(
[0] => 352
[1] => 324
[2] => 324
)
324 * 1.08 = 350.
352 + 324 * 2 = 1000.
Let $T is your total X, $n is a number of parts and $K is percentage mentioned above. Than
$x1 = $T / $n + $T * $K / 100;
$x2 = $x3 = .. = $xn = ($T - $x1) / ($n - 1);
Applied to your example:
$x1 = 1000 / 3 + 1000 * 0.03 = 363.3333333333333333333333333333
// you could round it if you want
// lets round it to ten, as you mentioned
$x1 = round($x1, -1) = 360
$x2 = $x3 = (1000 - 360) / 2 = 320
Extra for the first piece W = X*K/100
Remaining Z = X-W
Each non-first piece = Z/Y = (X-W)/Y = (100-K)*X/(100*Y)
The first piece = W + (100-K)*X/(100*Y) = X*K/100 + (100-K)*X/(100*Y)

Percentage a Number is of Another Number

How to find the percentage a number is of another number in PHP?
Example
$num1 = 2.6;
$num2 = 2.6;
// Should equal to 100%
Divide the two numbers and multiply by 100 to get the percentage:
$percentage = ($num1 / $num2) * 100;
echo $percentage . "%";
Of course you will need to check so that $num1 is not 0, something like this: $percentage = ($num1 !== 0 ? ($num1 / $num2) : 0) * 100;
$percentage = sprintf("%d%%", $num1 / $num2 * 100); // string: 100%

PHP formula needed to calculate a simple format

I am trying to take an amount and convert it to a units type format...
For example:
( note: don't worry about the dollar sign )
Total is $400
I need to display it as 4 * 100
Another Example
Total is $450
I need to display it as 4 * 100 | 50 * 1
So another words there are only 100 and 1 units.
I was thinking for 3 hours already and nothing seems to come to mind...Perhaps someone out there has done something similar and already know the answer?
Hoping I am not doing your homework. Try this:
$num = 450;
$ones = $num % 100;
$hundreds = floor($num / 100);
echo "$hundreds * 100 | $ones * 1";
Here's a simple implementation
$amount = 450;
$hundreds = floor($amount / 100);
$ones = $amount % 100;
$string = array();
if( $hundreds )
$string[] = "$hundreds * 100";
if( $ones )
$string[] = "$ones * 1";
echo implode(' | ', $string);
Check out the modulus (%) operator
Here's a simple solution which will only show the units present, you can get rid of the array/join stuff if you always need to show both units:
$total = 400;
$out = array();
$hundreds = floor($total / 100);
if ($hundreds) {
$out[] = $hundreds . ' * 100';
}
$ones = $total % 100;
if ($ones) {
$out[] = $ones . ' * 1';
}
echo join(' | ', $out);
Use the modulus operator to break down the number (this kind of thing is good to learn how to do because you'll need it for many other units conversion tasks like seconds -> minutes & seconds conversion):
$value=450;
$ones = $value % 100;
$hundreds = floor($value / 100);
echo "$hundreds * 100 | $ones * 1\n";

Categories