The combination of floor function and ceil function can make the data processed by PHP more real and reliable.
1. Let’s look at the floor function first:
Syntax:
float floor (float value)
Description:
Returns the next integer 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.
floor() example 1
<?php echo floor(1.6); // will output "1" echo floor(-1.6); // will output "-2" ?>
floor() example 2
<?php echo(floor(0.60)); echo(floor(0.40)); echo(floor(5)); echo(floor(5.1)); echo(floor(-5.1)); echo(floor(-5.9)) ?>
Output:
0
0
5
5
-6
-6
2. Ceil function:
Syntax:
float ceil (float value)
Description:
Returns the next integer that is not less than value. If value has a decimal part, it is rounded up by one digit. The type returned by ceil() is still float because the range of float values is usually larger than that of integer.
ceil() Example:
<?php echo ceil(4.3); // 5 echo ceil(9.999); // 10 echo ceil(-3.14); // -3 ?>
See the difference between these two functions? .
We will often use it when paginating
//Page number calculation:
$lastpg=ceil($totle/$displaypg); //最后页,也是总页数,用ceil就方便多了。 $lastpg=$lastpg ? $lastpg : 1; //没有显示条目,置最后页为1 $page=min($lastpg,$page); $prepg=$page-1; //上一页 $nextpg=($page==$lastpg ? 0 : $page+1); //下一页 $firstcount=($page-1)*$displaypg;
Of course, if you need to specify the accuracy, you need to use the round function.
3. round function:
Syntax:
float round ( float val [, int precision] )
Description:
Returns val rounded to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).
round() example
<?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 ?>
4. intval—convert variables into integer types
Convert the variable to integer type.
Syntax: int intval(mixed var, int [base]);
Return value: integer
Function type: PHP system function
Content Description
This function can convert variables into integer types. The omitted parameter base is the base of the conversion, with a default value of 10. The converted variable var can be any type variable except an array or class.
Example intval()
<?php echo intval(4.3); //4 echo intval(4.6); // 4 ?>
Note: If intval is a character type, it will be automatically converted to 0, such as
intval('abc');
Output result 0
If yes
intval('5fd');
The output result is
5
float ceil (float value) ceil returns the smallest integer that is not less than value. The return value is still float type int intval (mixed value [, int base]) intval is used to obtain the integer value of the variable, through base conversion (default is decimal), returns the integer value of the variableecho intval(42); // 42
echo intval(4.2); // 4
echo intval('42'); / / 42
echo intval('+42'); // 42
echo intval('-42'); // -42
echo intval(042); // 34
echo intval ('042'); // 42
echo intval(1e10); // 1410065408
echo intval('1e10'); // 1
echo intval(0x1A); // 26
echo intval(42000000); // 42000000
echo intval(42, 8); // 42
echo intval('42', 8); // 34
?>
ceil is rounded to the nearest integer, intval gets the integer value of the variable
ceil(3.33)=4;
intval(3.33)=3;
ceil(3.9)=4;
intval (3.9)=3;