Home >Backend Development >PHP Tutorial >Summary of PHP rounding methods_PHP tutorial
Summary of php rounding methods:
(1) Round to the larger one: ceil() - Get the next integer that is not less than the parameter. If there is a decimal part, round it up by one. The return type is float
1
2
//Take the bigger one
3
$c_1 = ceil(3.4);
4
echo $c_1.'
';
5
6
$c_2 = ceil(3.5);
7
echo $c_2.'
';
8
?>
Result:
(2) Round to the smallest integer: floor() - Get the next integer that is no larger than the parameter. If there is a decimal part, it will be discarded. The return type is float
1
2
//Take the smaller one
3
$f_1 = floor(3.4);
4
echo $f_1.'
';
5
6
$f_2 = floor(3.5);
7
echo $f_2.'
';
8
?>
Result:
(3) Rounding method: round() - round floating point numbers. The function has two parameters. The second parameter can set the number of decimal places retained in the result
01
02
//Rounding method
03
$r_1 = round(3.4);
04
$r_2 = round(3.5);
05
echo $r_1.'
';
06
echo $r_2.'
';
07
08
$r_3 = round(3.444, 2);
09
$r_4 = round(3.445, 2);
10
echo $r_3.'
';
11
echo $r_4.'
';
12
?>
Result:
(4) Forced conversion method: intval() - Convert the variable to an integer type. The function has two parameters. The first parameter can be any type of variable other than an array or class, and the second parameter sets the first The base of the parameter, and then convert the number in that base into a decimal integer
01
02
//Force conversion
03
$i_1 = intval(3.4);
04
echo $i_1.'
';
05
06www.2cto.com
$i_2 = intval(3.5);
07
echo $i_2.'
';
08
09
$i_3 = intval(a, 16);
10
echo $i_3.'
';
11
?>