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
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.
I have an array and I am getting its length using sizeof(). In the size of the array I want to get how many 10 there are in that number. I used % in getting my number like:
$arraysize = 13;//sizeof($array)
$numberoften = $arraysize % 10 // getting 3
From here I know that I am wrong. What i want to get is 2. Because I want to get every 10. Like if length is 9 I want to get 1. If length is 31 I want to get 4.
What is the correct function of way to get my desired output?
What you're after is ceil()
Returns the next highest integer value by rounding up value if necessary.
$numberoften = ceil($arraysize / 10);
There's also floor() if you want to round the number down to the nearest integer. This would seem to fit better with your question...
I want to get how many 10 there are in that number
Use ceil() function
$arraysize = 13;//sizeof($array);
$numberoften =ceil( $arraysize/10);
Or
Use round() function
$arraysize = 13;//sizeof($array);
$numberoften =round( $arraysize/10);
In PHP, I need to round off a number to the next whole number. For example, in my program, I am using the round as below.
$val = round(count($names)/15);
If count($names) is 1.2, I need it to be rounded off to 2 instead of 1. I tried to do the below approach.
$val = round(count($names)/15) + 1;
However, in the above approach, if I have count($names) as 1.6, It is getting rounded off to 3 as I increment it by 1.
Is there a way where no matter what the decimal value is, it needs to be rounded off to the next whole number?
How about using ceil()
http://php.net/manual/en/function.ceil.php
Then your code becomes:
$val = ceil(count($names)/15);
Try This:
$val = round(count($names)/15, PHP_ROUND_HALF_DOWN) + 1;
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
How to convert float percent number value to integer percent number value in PHP?
Example: 3.8461538461538 % = 4 %
Update:
Here is my code , I coded to get percent value, but I get decimal value, but I need it without decimal value.
$total = array_sum($total_count);
$percent = ($occurs/$total)*100;
echo $percent ;
echo "%";
This is done with round() - Rounds to closest int
<?php
echo round('3.8461538461538'); //4
?>
There is also:
floor() -- Round fractions down.
ceil() -- Round fractions up.
use round() . it's a PHP function.
Edit:
Given Code:
$total = array_sum($total_count);
$percent = ($occurs/$total)*100;
echo round($percent). "%";
For eg:
$number="3.8461538461538%"; //if it's a string with % in the end.
$number=substr($number, 0,-1); //It type converts automatically here. Lazy code :)
$number_rounded=round($number);
or
$number=3.8461538461538; //If it's just a float.
$number_rounded=round($number);
<?php number_format(3.8461538461538); ?>
You can specify in second argument the number of decimal points (default is 0).