I have no idea why this simple mathematical equation doesn't work. It's making me crazy - am I blind? Instead of subtracting $number from $Amount2 it produces a negative number. So if $Amount2 starts with a value of 100 and $number is a value of 10 I get -10 instead of 90 as the result.
//find how much green they are holding
$resultto = db_query("SELECT * FROM Users_Panickles WHERE PSID ='$PSID'");
$rowgifter = db_fetch_array($resultgifter);//fetch the name of the giftee character
$Amount2 = $resultgifter["Amount"];
//now alter the accounts
$Amount2 = $Amount2 - $number; //subtract the gift from the gifter's account and update the record
db_query ("UPDATE Users_Panickles SET Amount='$Amount2' WHERE PSID = '$PSID'");
Related
This question already has answers here:
Negative & Positive Percentage Calculation
(6 answers)
Closed 1 year ago.
I'm trying to work out a percentage between two numbers by providing the number of which the percentage should be based (can't find this on Stack overflow - only percentage between two numbers).
Let me explain:
$start_number = 14700
$end_number = 14900
// This is the number to calculate a percentage for
// in this basic example this should come to 50%
$percentage_number = 14800
I know what I need to do but can't figure out how to write it:
Where $start_number is 0%
And $end_number is 100%
$percentage_number = X%
$difference = $end_number - $start_number; // 200
$difference_from_start = $end_number - $percentage_number; // 100
$percentage = $difference_from_start / $difference; // 0.5 (50%)
Is it what you want?
Well this is quite simple:
$start_number = 14700;
$end_number = 14900;
$percentage_number = 14800;
$result = (($end_number - $percentage_number) * 100) / ($end_number - $start_number);
echo $result;
OUTPUT:
50
I thik that the solution formula would be:
percentage = 100 * (percentage_number - start_number) / (end_number - start_number)
In that case $percentage_number should not be less than $start_number else the percentage will calculated in negative.
You can use the below formula:
( ($percentage_number - $start_number) / ($end_number - $start_number) ) * 100 ;
Hope it'ill help you.
Let's say you have two numbers, 40 and 30.
30/40*100 = 75.
So 30 is 75% of 40.
40/30*100 = 133.
So 40 is 133% of 30.
The percentage increase from 30 to 40 is:
(40-30)/30 * 100 = 33%
The percentage decrease from 40 to 30 is:
(40-30)/40 * 100 = 25%.
These calculations hold true whatever your two numbers.
In PHP
$oldFigure = 14;
$newFigure = 12.50;
$percentChange = (1 - $oldFigure / $newFigure) * 100;
echo $percentChange;
diff = $end_number - $start_number
(diff is now = 200)
mid = $end_number - (your number)
(mid is now = 100)
res = (mid / diff) * 100
(res is now = 50%)
If the scale begins at $start_number and ends at $end_number, its size is $end_number-$start_number. If $percentage_number is in the scale, it's $percentage_number-$start_number units from the start into the scale. Therefore, the relative ratio in the scale being:
($percentage_number-$start_number)/($end_number-$start_number)
Multiply by 100 and you'll get the percentage.
First post, please be gentle.
I'm trying to create a simple market script where for example I have a number in my database ie 50.00 and I want to run a cron job php script to increase or decrease this randomly to a minimum of 10.00 and a maximum of 75.00.
I thought a random 0,1 follow by 2 if statements 1 rand(-0.01,0.05) if 2 rand(0.01,0.05) then $sql = "UPDATE price SET oil='RESULT'";
I've tried a few times at the above but I can't get it to run and the other crons in the file work.
<?php
//Get Oil Price from database
$oilchange = rand(1, 2);
if ($oilchange == '1') {
$oilnew = rand(0.01,0.05);
//Oil price from database times oil new.
} else {
$oilnew = rand(-0.01,-0.05);
//Oil price from database times oil new.
}
// Update Price
?>
Rand is for integers (whole numbers)
First up, your use of rand between two decimal values (called floats) won't work, as rand is for integers only. So, you'd first want to have a random function which does output floats, like this:
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
Then we can safely use it between, say, 1% and 5%:
$percentSwing = randomFloat(0.01, 0.05);
Rand defaults to being 0 or 1. We can use that to randomly invert it, so we also cover -1% to -5%:
$percentSwing *= rand() ? 1 : -1;
The above could also be written like this:
if(rand() == 1){
// Do nothing:
$percentSwing *= 1;
}else{
// Invert it:
$percentSwing *= -1;
}
So, we now know how much we need to swing the number by. Let's say it was $oilPrice:
$oilPrice = 48;
We can just multiply the percent swing by that number to get the amount it's changing by, then add it back on:
$oilPrice += $percentSwing * $oilPrice;
So far so good! Now we need to make sure the price did not go out of our fixed range of 10 to 75. Assuming you want to 'clamp' the number - that means if it goes below 10, it's set at 10 and vice-versa, that's done like this:
if( $oilPrice < 10 ){
// It went below 10 - clamp it:
$oilPrice = 10;
}else if( $oilPrice > 75 ){
// It went above 75 - clamp it:
$oilPrice = 75;
}
The above can also be represented in one line, like this:
$oilPrice = max(10, min(75, $oilPrice));
So, that gives us the whole thing:
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
// Define the oil price (e.g. pull from your database):
$oilPrice = 48;
// get a random 1% to 5% swing:
$percentSwing = randomFloat(0.01, 0.05);
// Invert it 50% of the time:
$percentSwing *= rand() ? 1 : -1;
// Swing the price now:
$oilPrice += $percentSwing * $oilPrice;
// Clamp it:
$oilPrice = max(10, min(75, $oilPrice));
// Output something!
echo $oilPrice;
As a side note here, money in real financial systems is never stored as a float, because rounding errors can cause major problems.
I have tried to do this myself, it seems fairly simple i must be over complicating it, i have the following code:
// get the % we make
$row = DB::getInstance()->selectValues('SELECT * FROM `profit`');
$per = $row['profit_percentage'];
$pro = $per * 100 - ((str_replace("$", " ", trim($offer->children('campinfo', true)->amount) / 100)));
The value of $per = 60 this value $offer->children('campinfo', true)->amount can contain any value like: 0.55 or 25.34 what i'm trying to do is deduct 60 (the percentage) off the value of $offer->children('campinfo', true)->amount
I'm going round in circles
any help would be appreciated.
This boils down to simple math:
Giving a 60% discount is the same as only charging 40%.
So all you have to do is calculate $price * 0.4.
Pro tipp: Don't store your percentage as {0..100}, but as {0..1}. That way you can rewrite your code as (1 - $per) * $price!
I do not understand something! I want to create a query ... in the database under the following entry field stamp_updated, 2014-01-28 17:31:02.
Now I just want to have records with the tag 28:
$ qry = "SELECT SUM (bytes) AS total FROM acct_v4 WHERE SUBSTRING (stamp_updated, 9,10) = '28 '";
Unfortunately, I get no output, it's the SUBSTRING?
PS:
What is supposed to be: I logged the port traffic, let him write in a database and would like to have daily statistics.
done ask new
Now I stand in front of logig proplem ... how do I put it best when I try to spend as Everyday Traffic?
2014-01-26: xx traffic 2014-01-27: xx traffic 2014-01-28: xx traffic
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
function formatBytes($size, $precision = 2)
{
$base = log($size) / log(1024);
$suffixes = array('', 'k', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
$db=mysql_connect("localhost","traffic",".....");
mysql_select_db("traffic",$db);
$qry = "SELECT SUM(bytes) AS total FROM acct_v4
WHERE SUBSTRING(stamp_updated,9,2) = '28' ";
$select = mysql_query($qry);
$result = mysql_fetch_array($select);
echo 'Summe: '.formatBytes($result['total']);
//echo $result['total'];
?>
You can try
SELECT SUM (bytes) AS total FROM acct_v4 WHERE DAY(stamp_updated) = 28;
your SUBSTRING (stamp_updated, 9,10) will return 10 charcters after the position 9
--> 28 17:31:0
this is the basic of SUBSTRING
SUBSTRING(str, pos, len)
so in your case you should just change 10 to 2 like that
SUBSTRING (stamp_updated, 9,2)
and it will work
your whole query will be
$ qry = "SELECT SUM(bytes) AS total FROM acct_v4
WHERE SUBSTRING(stamp_updated,9,2) = '28' ";
Without seeing your SHOW CREATE TABLE, it looks as though you are after dates with 28 for the day. You should use MySQL's DAY function. This assumes that stamp_updated is a proper DATE or DATETIME field.
$qry = "SELECT SUM (bytes) AS total FROM acct_v4 WHERE DAY(stamp_updated) = 28";
I am getting a rounding issues, I have a database of 3 charities with different amounts.. then in the front I have the 3 charities displaying there percentage based on the amount assigned to them and the total percentage always needs to add to 100%.. currently if each charity has 1 assigned to it, it displays 33% on each which equals 99% where i need to cheat it in a way to always be equal to 100%..
Here is the PHP
$charity = $_POST['charity'];
$sql = "SELECT amount FROM charities";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
$total += $row['amount'];
}
$sql_individual = "SELECT * FROM charities WHERE charity='$charity'";
$result_individual = mysqli_query($con, $sql_individual);
while($row = mysqli_fetch_array($result_individual)) {
$charity = $row['amount'];
}
echo round($charity / $total * 100,0) . "%";
I found a maths solution for this.. but.. my maths isn't all that great.. but.. okay in all honesty i do not understand this fully:
c = 100 - (a + b) - e,g 34 = 100 - (33 + 33)
any Help Greatly Appreciated..
There are no rounding issues, You just round to an integer numbers, that means that if You have 5 values like these:
12,25%
13,25%
20,25%
30,25%
34,00%
after using round($x, 0) on them they will be rounded to these values:
12
13
20
30
34
After summing these rounded values You get the value of 99.
It is best to round at least to two decimal places when working with percentages - and only for displaying purposes. You shouldn't round any values that will be used for further math operation...