Home >Backend Development >PHP Tutorial >Four commonly used methods for taking integer functions in PHP

Four commonly used methods for taking integer functions in PHP

怪我咯
怪我咯Original
2017-06-19 11:09:495177browse

PHPGetIntegerFunctionFour commonly used methods, four functions are collected below; rounding functions are often used, today’s small in conclusion! In fact, it is very simple, just a few functions ~~ Mainly: ceil, floor, round, intval

ceil - rounding to the nearest integer
Explanation
float ceil (float value)
Returns the next integer that is not less than value. If value has a decimal part, it will be rounded up by one. The type returned by ceil() is still float because the range of float values ​​is usually larger than integer.
Example 1. ceil() example

The code is as follows:

<?php 
echo
 ceil(4.3); // 5 
echo ceil(9.999); // 10 
?>

floor - rounding by rounding
Explanation
float floor (float value)
Returns the next integer that is not greater than value, and rounds the decimal part of value. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.
Example 1. floor() Example

The code is as follows:

<?php 
echo floor(4.3); // 4 
echo floor(9.999); // 9 
?>

round — Round floating point numbers
Explanation
float round (float val [, int precision])
Returns the result of rounding val according to the specified precision precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).
Example 1. round() example

The code is as follows:

<?php 
echo round(3.4); // 3 
echo round(3.5); // 4 
echo round(3.6); // 4 
echo round(3.6, 0); // 4 
echo round(1.95583, 2); // 1.96 
echo round(1241757, -3); // 1242000 
echo round(5.045, 2); // 5.05 
echo round(5.055, 2); // 5.06 
?>


intval—convert the variable into an integer type
Example intval()

The code is as follows:

<?php 
echo intval(4.3); //4 
echo intval(4.6); // 4 
?>

The above is the detailed content of Four commonly used methods for taking integer functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

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