In this article, we will introduce four common functions in php: ceil, floor, round, and intval. Ceil-floatceil (floatvalue)
This article introduces four common functions in php: ceil, floor, round, and intval.
Ceil-integer in one method
Description
Float ceil (float value)
Returns the next integer not less than the value. if the value has a decimal part, the type returned by one. ceil () is still float, because the float value range is usually larger than integer.
Example 1. ceil () example
The instance code is as follows:
-
- Echo ceil (4.3); // 5
- Echo ceil (9.999); // 10
- ?>
We usually use
// Page number calculation:
The instance code is as follows:
- $ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total number of pages
- $ 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;
Floor-rounding
Description
Float floor (float value)
Returns the next integer not greater than the value. rounds the fractional part of the value to an integer. floor () returns the float type, because the float value range is usually larger than integer.
Example 1. floor () example
Example
In this example, we will apply the floor () function to different numbers:
The instance code is as follows:
-
- Echo (floor (0.60 ));
- Echo (floor (0.40 ));
- Echo (floor (5 ));
- Echo (floor (5.1 ));
- Echo (floor (-5.1 ));
- Echo (floor (-1, 5.9 ))
- ?>
Output:
0
0
5
5
-6
-6
Round-rounding floating point numbers
Description
Float round (float val [, int precision])
Returns the result of rounding val by the specified precision (number of digits after decimal point). The precision can also be a negative number or zero (default ).
Note: PHP cannot properly process strings similar to "12,300.2" by default.
Note: The prec parameter is introduced in PHP 4 ..
The instance code is as follows:
-
- Echo (roround (0.60 ));
- Echo (roround (0.50 ));
- Echo (roround (0.49 ));
- Echo (round (-4.40 ));
- Echo (round (-4.60 ));
- ?>
Output:
1
1
0
-4
-5
Intval-convert a variable to an integer type
Variable to integer type.
Syntax: int intval (mixed var, int [base]);
Return value: integer
Function types: PHP system functions
Description
This function can convert a variable to an integer type. the base parameter that can be omitted is the basis of the conversion. the default value is 10. the transformed variable var can be any type variable other than the array or class.
Example intval ()
The instance code is as follows:
-
- 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