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;
Related
I was trying to run average in my project but I was not able to make the round function work, every time I run the function I always end up with float value example: average is 86.5, I wanted it to round to nearest tens which is 87 or if lower it will turn 86.
Controller
$score->average =round( $row['result1'] + $row['result2']) /2;
Schema
$table->integer('result1');
$table->integer('result2');
You can get integer value like this:
$score->average = (int)round(($row['result1'] + $row['result2']) /2);
Since, round function returns floating point value like 3.0, 45.0 so to get integer you have to type case the value.
Can you try this one
$score->average =round( ($row['result1'] + $row['result2']) /2) ;
using this will apply the PEMDAS rule.
Hope it helps
You should try this:
$summation = $row['result1'] + $row['result2'];
$divideRslt = $summation / 2;
$score->average =round($divideRslt);
This might seem like a very simple thing, and maybe I'm just not understanding the question? Anyway. $a and $b are random numbers from 1000 to 10000. I need to take the largest one and round it to the nearest hundred.
Is this supposed to be like so:
$a = 5;
$b = 10;
echo mt_rand($a,$b);
But then, how do I get the largest number and round it?
I don't know why this confuses me so much.
Also, apparently, I am supposed to use max somewhere.
You need to add the values to an array then use max of the array.
To round the value to closest 100 you need to use the following formula round(x/100)*100
This example makes 10 random numbers and echo the max value and the rounded max value.
For($i=0;$i<10;$i++){
$a[] = mt_rand(1000,10000);
}
//Var_dump($a);
Echo max($a). "\n"; // max number
Echo round(max($a)/100)*100; //max number rounded
https://3v4l.org/NRWiH
I am trying to do a 2 digit precision in PHP Laravel project but it doesnt work. I have the value 1234666.6666667 that I want to make 1234666.66 but all the results I've seen in here or/and in other search pages.
This is my code:
$value = 1234666.6666667;
return round($value,2);
any other solution?
EDIT:
As I see, you actually want to floor number to 2 decimal points, not to round it, so this answer could help you:
$value = 1234666.6666667;
floor($value * 100) / 100; // returns 1234666.66
If you want 3 decimal points you need to multiple and divide with 1000, for 4 - with 10000 and etc.
You can use number_format, it convert value to string though, so you lose real float value:
$value = 1234666.6666667;
echo number_format($value, 2, '.', ''); // prints 1234666.67
Use this function.
function truncate($i) {
return floor($i*100) / 100.0;
}
Then you can do
$value = truncate(123.5666666); // 123.56
A pragmatic way is to use round($value - 0.05, 2), but even that gets you into hot water with some edge cases. Floating point numbers just don't round well. It's life I'm afraid. The closest double to 1234666.66 is
1234666.65999999991618096828460693359375
That's what $value will be after applying my formula! Really, if you want exact decimal precision, then you need to use a decimal type. Else use integer types and work in multiples of 100.
For the former choice, see http://de2.php.net/manual/en/ref.bc.php
$value = bcadd($value, 0, 2); // 1234666.6666667 -> 1234666.66
Another more exotic way to solve this issue is to use bcadd() with a dummy value for the $right_operand of 0,
This will give you 2 number after decimal.
Is it possible to round a number where if it's .5, just leave it, anything below .5 round down, anything above .5 round up?
For example:
5.0 * 1.35 = 6.75 // leave it
5.2 * 1.35 = 7.02 // round down to 7.00
5.5 * 1.35 = 7.56 // round up to 8.00
I've formatted with round($n,0, PHP_ROUND_HALF_UP) where $n is the product from the above calc , which leaves 6.75 but returns 7.02 for the next one. I also tried round($n,-1, PHP_ROUND_HALF_UP) which gives me the 7.00 on the second calc but then of course won't return a 6.75 for the first, instead it returns 680.
This is a ticket markup calculation where the user enters the first number and is multiplied by the second. I actually remove the decimal because they don't want to see it, and they want this sort of customized rounding on the result.
function myround($num, $prec) {
$rhu = round($num, $prec, PHP_ROUND_HALF_UP);
$rhd = round($num, $prec, PHP_ROUND_HALF_DOWN);
return ($rhu + $rhd) / 2;
}
Works for any precision you like. For hundreth's place, as in the example, $prec would need to be 2.
The only way to determine the value of the last non-zero digit of a given floating point number in PHP is to convert it to a string.
$str = (string) $float;
$result = ($str[strlen($str) - 1] == 5) ? $float : round($float);
Example
Of course, no matter what you do it will be subject to a small margin of error because of the floating point precision issue.
$n = round($n, 2);
if($n % .05 != 0 || $n % .1 == 0)
{
$n = round($n);
}
Does this work for you? I'm assuming the 5 you speak of is the hundredth digit, and if it's not 5 then you want a whole number.
How can I separate a number and get the first two digits in PHP?
For example: 1345 -> I want this output=> 13 or 1542 I want 15.
one possibility would be to use substr:
echo substr($mynumber, 0, 2);
EDIT:
please not that, like hakre said, this will break for negative numbers or small numbers with decimal places. his solution is the better one, as he's doing some checks to avoid this.
First of all you need to normalize your number, because not all numbers in PHP consist of digits only. You might be looking for an integer number:
$number = (int) $number;
Problems you can run in here is the range of integer numbers in PHP or rounding issues, see Integers Docs, INF comes to mind as well.
As the number now is an integer, you can use it in string context and extract the first two characters which will be the first two digits if the number is not negative. If the number is negative, the sign needs to be preserved:
$twoDigits = substr($number, 0, $number < 0 ? 3 : 2);
See the Demo.
Shouldn't be too hard? A simple substring should do the trick (you can treat numbers as strings in a loosely typed language like PHP).
See the PHP manual page for the substr() function.
Something like this:
$output = substr($input, 0, 2); //get first two characters (digits)
You can get the string value of your number then get the part you want using
substr.
this should do what you want
$length = 2;
$newstr = substr($string, $lenght);
With strong type-hinting in new version of PHP (> PHP 7.3) you can't use substr on a function if you have integer or float. Yes, you can cast as string but it's not a good solution.
You can divide by some ten factor and recast to int.
$number = 1345;
$mynumber = (int)($number/100);
echo $mynumber;
Display: 13
If you don't want to use substr you can divide your number by 10 until it has 2 digits:
<?php
function foo($i) {
$i = abs((int)$i);
while ($i > 99)
$i = $i / 10;
return $i;
}
will give you first two digits