Home > Backend Development > PHP Tutorial > ceil, floo and round function in php_PHP tutorial

ceil, floo and round function in php_PHP tutorial

WBOY
Release: 2016-07-15 13:21:31
Original
1207 people have browsed it

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477158.htmlTechArticleceil 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 Function used for rounding. ceil definition and usage: ceil() function rounds upward...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template