I am new at programing and I am trying to figure out how to write a variable correctly. To calculate the number for this variable I have to divide by 10 and if the result does not evenly divide, I need to add 1 on to it.
So for example, lets I have to divide 294 / 10, I would get 29.4. In this case I would want to add 1, which would set the variable to 30. But if I was dividing 200 by 10, I would not need to add 1 because it would be an even 20.
So currently I have the variable like this:
$total = $count / 10;
How would I adjust it to set correctly in cases that it does not even .0
It sounds like you're trying to round the value up. There's a built-in function for this purpose -- ceil().
From the function description:
Returns the next highest integer value by rounding up value if necessary
Usage:
$count = 294;
echo ceil($count / 10); // => 30
Make use of ceil() in PHP
$count=294;
$total = ceil($count / 10); // your variable $total now holds the value of 30
You want to use a function called ceil
$total = ceil($count / 10);
http://www.php.net/manual/en/function.ceil.php
$total = $count / 10;
if(!is_int($total)) {
$total = ceil($total);
}
else {
// if you want to make some actions if number is exactly .0
}
You can use ceil() function for this.
$total = $count / 10;
echo ceil($total);
For more details refer http://php.net/ceil
Related
How to extract the percentage from the array
The result is as follows: 41 - 16 - 8 - 33
Total is 98, not 100
How to make it = 100
$sum = array(500.36,200.32,100.09,400);
$total = array_sum($sum);
foreach($sum as $val){
$st = intval($val / $total * 100 );
echo $st.'<br>';
}
The reason is: precision :)
With intval() you skip the decimals. so 41+16+8+33 is REALLY 98.
If you add them with 2 decimals:
41.66 + 16.68 + 8.33 + 33.31 = 99.98
If you do round() instead of intval() you'll round the values so it'll be more close statistically. you'll get: 42 + 17 + 8 + 33 = 100
BUT! if you want to make sure the sum is 100, than you should pick one number (I suggest the biggest one) to calculate that as: 100 - sum(the rest).
Rounding!
Take the number 500.36;
500.36 / 1200.77 * 100:
41,669928463
Since you're using intval those deciamals are lost, remove intval to keep the decimals, then you'll reach 100 total.
Consider an extra variable to check this;
<?php
$sum = array(500.36,200.32,100.09,400);
$total = array_sum($sum);
$test = 0;
foreach($sum as $val){
$st = $val / $total * 100;
echo $st.'<br>';
$test += $st;
}
echo $test;
41.66992846257
16.682628646618
8.3354847306312
33.311958160181
100
Try it online!
you are using intval(). you should use round() with the precision of 2. you are skipping fractions by converting answer to integer. PHP is using floor method so your answer is losing fractions. here is the code
$sum = array(500.36,200.32,100.09,400);
$total = array_sum($sum);
foreach($sum as $val){
$st = round($val / $total * 100, 2 );
echo $st.'<br>';
}
If you make an intval to the result you cut the last values.
Without intval you can see the problem.
41.66992846257
16.682628646618
8.3354847306312
33.311958160181
So work with that numbers or round it on 2 decimal numbers.
$st = number_format($val / $total * 100, 2, '.', '');
something like this.
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.
There is this function that is used tat I didn't myself create, but at the moment it is only returning the number rounded with 2 decimal places, but I want to change it so it returns it with three; however I don't really understand how it all works.
Here is the function:
function round_number($number, $round = 2)
{
// we will multiply by 10^$round, then get the floor value of that amount then divide by 10^round.
## -> if it does problems, switch back to floor()
$temp_value = $number * pow(10, $round);
$temp_value = (!strpos($temp_value, '.')) ? $temp_value : floor($temp_value);
$number = $temp_value / pow(10, $round);
return $number;
}
I assume if I change the $round to 3 that it will return correctly?
// we will multiply by 10^$round, then get the floor value of that amount then divide by 10^round.
## -> if it does problems, switch back to floor()
It says it right in the code what it does!
And yes - changing $round = 3; will work.
However DON'T you should instead just call the function
round_number(12345.12342423, 3);
the number passed in as second parameter (3) will override the $round=2 in the function ($round=2 is the 'default')
I need to get a random number that is between
0 - 80
and
120 - 200
I can do
$n1 = rand(0, 80);
$n2 = rand(120, 200);
But then I need to choose between n1 and n2. Cannot do
$n3 = rand($n1, $n2)
as this may give me a number between 80 - 120 which I need to avoid.
How to solve this?
Since both ranges have different sizes (even if only by 1 number), to ensure good random spread, you need to do this:
$random = rand( 0, 200 - 39 );
if ($random>=120-39) $random+=39;
Fastest method. :)
The way this works is by pretending it's a single range, and if it ends up picking a number above the first range, we increase it to fit within the second range. This ensures perfect spread.
Since both ranges have the same size you can simply use rand(0, 1) to determine which range to use.
$n = rand(0, 1) ? rand(0, 80) : rand(120, 200);
PHP has a new function for this as well called range. Very easy to use, and can be located in the PHP Manual.
It allows you to input a minimum/maximum number to grab a range from.
<?php
echo range(0, 1000);
?
Technically though, you could also enter your own two numbers to serve as the number range.
get two random numbers $n1 and $n2
$n1 = rand(0, 80);
$n2 = rand(120, 200);
define new array called $n3
$n3=array();
add $n1 and $n2 into array $n3 use array_push() function
array_push($n3,$n1,$n2);
use array_rand() function to find random index $find_index from array $n3.
$find_index=array_rand($n3,1);
show the result
echo $n3[$find_index];
I am getting a max value through value. It can be any digit (whole number). I want it to take to next tenth-value, using PHP
Example:
If value is 66, then I need value 70
If value is 6, then I need value 10
If value is 98, then I need value 100
This is an arithmetic problem:
y = ceil(x / 10) * 10
If you’re looking just for the nearest decade, use round instead.
$num = 66;
$val = ceil($num / 10) * 10;
echo $val;
Thanks.
or
echo round(66,-1);
up to 30 characters