When processing floating point numbers in php, it is often necessary to rounding them. In php, there are two functions that apply to this situation: The floor function, the ceil function, and the round function floor function and the ceil function work together to make the data processed by php more authentic and reliable.
I. First, let's look at the floor function:
Syntax:
Float floor (float value)
Note:
Returns the next integer not greater than value and rounds the decimal part of value. The return type of floor () is still float, because the float value range 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
<?phpecho(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
II. ceil functions:
Syntax:
Float ceil (float value)
Note:
Returns the next integer of no less than value. if the value has a decimal part, it is entered in one place. The type returned by ceil () is still float, because the float value range is usually larger than integer.
Ceil () example:
<?php echo ceil(4.3); // 5 echo ceil(9.999); // 10 echo ceil(-3.14); // -3 ?>
See the differences between the two functions ..
We usually use
// Page number calculation:
$ Lastpg = ceil ($ totle/$ displaypg); // The last page is the total number of pages. it is much easier to use ceil. $ Lastpg = $ lastpg? $ Lastpg: 1; // no entries are displayed. set the last page to 1 $ page = min ($ lastpg, $ page); $ prepg = $ page-1; // Previous page $ nextpg = ($ page = $ lastpg? 0: $ page + 1); // Next page $ firstcount = ($ page-1) * $ displaypg;
Of course, if you need to set the accuracy, you need to use the round function.
III. round functions:
Syntax:
Float round (float val [, int precision])
Note:
Returns the result of rounding val by the specified precision (number of digits after 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 ?>
IV. intval-convert a variable to an integer type
Convert a variable to an integer.
Syntax: int intval (mixed var, int [base]);
Return value: integer
Function types: PHP system functions
Description
This function converts a variable to an integer. The base parameter that can be omitted is the basis for conversion. the default value is 10. The converted variable var can be any type variable outside the array or class.
Example intval ()
<?php echo intval(4.3); //4 echo intval(4.6); // 4 ?>
Note: intval is automatically converted to 0 if it is of the optimized type. for example
Intval ('ABC ');
Output result 0
If yes
Intval ('5fd ');
The output result is
5