Home >Backend Development >PHP Tutorial >Usage and introduction of PHP rounding functions ceil, floo, round_PHP tutorial

Usage and introduction of PHP rounding functions ceil, floo, round_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:12:29881browse

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:
ceil() function rounds up to the nearest integer.

Copy code The code is as follows:

ceil(x);

Explanation :
Returns the next integer that is not less than x. If x has a decimal part, it will be rounded up.
The type returned by ceil() is still float.

Example:

Copy code The code is as follows:

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:
Copy code The code is as follows:

1
1
5
6
-5
-5

floor
Definition and usage: The
floor() function rounds down to the nearest integer.
Copy code The code is as follows:

floor(x);

Explanation :
Returns the next integer not greater than x, and rounds the decimal part of x.
The type returned by floor() is still float.

Example:

Copy code The code is as follows:

echo(floor(0.60));
echo "
";
echo(floor(0.40));
echo "
";
echo( floor(5));
echo "
";
echo "
";
echo(floor(5.1));
echo "< br/>";
echo(floor(-5.1));
echo "
";
echo(floor(-5.9))
?>

Output:
Copy code The code is as follows:

0
0
5
5
-6
-6

round
Definition and Usage
round() function rounds floating point numbers.

Copy code The code is as follows:
round(x,prec);

where
x (optional) Specifies the number to be rounded.
prec (optional) specifies the number of digits after the decimal point.

Description:
Returns the result of rounding x according to the specified precision prec (the number of decimal digits after the decimal point).
prec can also be negative or zero (default).

Example:

Copy code The code is as follows:
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:
Copy code The code is as follows:
10
12
1
0
-1
-0

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313677.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 Definition and usage of the function ceil used for rounding: ceil() function rounds upward...
Statement:
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