I know the following code will remove the numbers are the point. round($number);
I want to round the numbers as follows if number is 20.123 I want result 20, If number is 20.567 I want result 21
Means if value is below .5 , it should remove that value. If value if .5 or above it should round up. How ? Anyone help ?
Try this:
use the ceil
, floor
, explode
and substr
function to achieve you value.
$num = "20.123";
$arr = explode(".", $num);
if(substr($arr[1], 0, 1) >= 5){
$num = ceil($num);
}else{
$num = floor($num);
}
echo $num;
Result:
20
Also you can use the round
function.
round(20.156); // 20
round(20.651); // 21
I am sure it's working well.
<?php
$var = 22.443;
$var = number_format($var, 0, '.', '');
echo $var;
?>
function get_decimal_num($number){
$num = explode('.', $number);
return $num[1];
}
$num = 10.5 ; // for example
$val = get_decimal_num($num);
if($val >= 5)
{
$value = (int) $num;
echo $value = $value + 1;
}
if($val < 5)
{
echo $num;
}
Plese check it...