Home >Backend Development >PHP Tutorial >ceil, floo and round function in php_PHP tutorial
ceil is a function that carries up to get a value;
Floor is a function that rounds off decimal places to get a value;
round is a function used for rounding.
ceil
Definition and usage:
The ceil() function rounds up to the nearest integer.
[php]
ceil(x);
ceil(x);
Description:
Returns the next integer that is not less than x. If x has a decimal part, it is rounded up.
The type returned by ceil() is still float.
Example:
[php]
echo ceil(0.60);
echo "
";
echo ceil(0.40);
echo "
";
echo ceil(5);
echo "
";
echo ceil(5.1);
echo "
";
echo ceil(-5.1);
echo "
";
echo ceil(-5.9);
?>
echo ceil(0.60);
echo "
";
echo ceil(0.40);
echo "
";
echo ceil(5);
echo "
";
echo ceil(5.1);
echo "
";
echo ceil(-5.1);
echo "
";
echo ceil(-5.9);
?>
Output:
[php]
1
1
5
6
-5
-5
1
1
5
6
-5
-5
floor
Definition and usage:
The floor() function rounds down to the nearest integer.
[php]
floor(x);
floor(x);
Description:
Returns the next integer not greater than x, rounding off the decimal part of x.
The type returned by floor() is still float.
Example:
[php]
echo(floor(0.60));
echo "
";
echo(floor(0.40));
echo "
";
echo(floor(5));
echo "
";
echo "
";
echo(floor(5.1));
echo "
";
echo(floor(-5.1));
echo "
";
echo(floor(-5.9))
?>
echo(floor(0.60));
echo "
";
echo(floor(0.40));
echo "
";
echo(floor(5));
echo "
";
echo "
";
echo(floor(5.1));
echo "
";
echo(floor(-5.1));
echo "
";
echo(floor(-5.9))
?>
Output:
[php]
0
0
5
5
-6
-6
0
0
5
5
-6
-6
round
Definition and Usage
The round() function rounds floating point numbers.
[php]
round(x,prec);
round(x,prec);
Which
x (optional) Specifies the number to be rounded.
prec (optional) specifies the number of digits after the decimal point.
Description:
Returns x rounded to the specified precision prec (the number of decimal digits after the decimal point).
prec can also be negative or zero (default).
Example:
[php]
echo round(12.345,-1);
echo "
";
echo round(12.345);
echo "
";
echo round(0.5);
echo "
";
echo round(0.4);
echo "
";
echo round(-0.5);
echo "
";
echo round(-0.4);
?>
echo round(12.345,-1);
echo "
";
echo round(12.345);
echo "
";
echo round(0.5);
echo "
";
echo round(0.4);
echo "
";
echo round(-0.5);
echo "
";
echo round(-0.4);
?>
Output:
[php]
10
12
1
0
-1
-0