Adding Percentage of Number to Itself - php

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));

Related

Get percentage between 2 numbers

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 %

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;

Add up numbers in a for each PHP?

I'm trying to add some numbers in a foreach loop in my PHP code. I am getting a percentage of numbers in a while loop in my MySQL query for each result that I get in my PHP page.
All I need to do is to add up the final values in and show them as total.
This is how I make up the percentage in my while loop in my MySQL query:
$percentage = 10;
$totalWidth = $fees;
$new_width = ($percentage / 100) * $totalWidth;
The $fees value is dynamic and it is different for each result in my while loop. the code above works as it should.
Now I want to add up all the values of $new_width. For example:
If one result's $new_width is 25 and the other one is 10 and another one is 5, I need to do this: $total = 25 + 10 + 5;
So I tried something like this:
$total = 0;
foreach($new_width as $var) {
$total = $var + $var;
}
echo $total;
but the above code doesn't really make sense and it won't do anything at all.
Could someone please advise on this matter?
First you want to change this line in your while loop so you get an array:
$new_width = ($percentage / 100) * $totalWidth;
to this:
//Declare it as an array before your while loop
$new_width = array();
//In your while loop
$new_width[] = ($percentage / 100) * $totalWidth;
//^^ See here
After this you just have to change the line in your foreach loop like this:
$total = $var + $var;
to this:
$total += $var;
(If you want you also can do this in your while loop)
If you have an array of numbers and you want to calculate the sum of those numbers, you should use array_sum().
According to the logic, you are setting the total to 2 X $var.
My answer is very similar, but you add it to the total which is outside of the loop and the value will keep growing:
$total = 0;
foreach($new_width as $var) {
$total += $var;
}
echo $total;
Or simply as stated before, if it is the only value in the array:
$total = array_sum($new_width);

Rounding a calculation up using php

How would I round this calculation up automatically??
$calc = (14.3/10)/2.33 Result = 6.137339055794
The value I would like to have is 6.13
Have searched the site but can't find any answers on this
Use PHP round().
$calc = round($calc, 2)
The result of (14.3/10)/2.33 is not 6.137339055794. It's 0.6137339055794.
I'm assuming: $calc = (14.3/10) / 2.33 * 10;.
The function round rounds half up, half down, half even or half odd numbers (floating point numbers), but the result of round($calc, 2, PHP_ROUND_HALF_UP) is not 6.13. It's 6.14.
Assuming you'd like to round down or truncate that number, a solution could be:
$truncated = (int)($calc * 100) / 100;
or
$precision = 2;
$tensPrecision = pow(10, $precision);
$truncated = (int)($calc * $tensPrecision) / $tensPrecision;
.
To round up, you could do:
$truncated = ceil($calc * 100) / 100;
or
$precision = 2;
$tensPrecision = pow(10, $precision);
$truncated = ceil($calc * $tensPrecision) / $tensPrecision;

PHP: Get thousand from number

When the user enters a number from 1000 and above I want to be able to get the thousand for that number in an array.
For example…
Number entered by user: 165124
My array should return:
array('thousand_low' => 165000, 'thousand_high' = 165999)
Thanks!
The complete array-returning function, using PHP's native floor and ceil functions:
function get_thousands($num) {
return array(
'thousand_low'=>floor($num/1000)*1000,
'thousand_high'=>ceil($num/1000)*1000-1
);
}
Untested (edit: but should work ;) ):
$number = 165124;
$low = floor($number / 1000) * 1000;
$high = $low + 999;
Something like this:
$num = 165124;
$result = array();
$result['thousand_low'] = floor($num / 1000) * 1000;
$result['thousand_high'] = $result['thousand_low'] + 999;
Have a look at the round function (http://php.net/manual/en/function.round.php) - you can specify the precision so you can customise the magnitude of the rounding.
array('thousand_low' => floor($int/1000)*1000,
'thousand_high' => floor($int/1000)*1000+999);
Haven't used php in quite a while but i think it should look something like this :
$num_input = 165124;
$array['thousand_low'] = floor($num_input / 1000) * 1000;
$array['thousand_high'] = $array['thousand_low'] + 999;

Categories