This article describes the rounding function that you often need to use when dealing with floating-point numbers in PHP. There are two functions in PHP that apply to this case: the floor function, the Ceil function, and the round function The floor function and the Ceil function match each other to make PHP processing data more authentic and reliable. Floor: To take the whole Ceil: Rounding, contrary to floor function Round: Select precision based on parameters (this is true rounding) Intval: Gets the integer value of the variable, and returns 0 if the argument is a string first, look at the floor function: Grammar: Float floor (float value) Description Returns the next integer not more than value, rounding out the decimal part of value. The type returned by floor () is still float, because the float value is usually larger than the integer. Floor () Example 1 <?php
Echo Floor (1.6);//would output "1"
echo floor ( -1.6);/would 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 Second, the Ceil function: Grammar: float ceil (float value) Description Returns the next integer that is not less than value, or the value if there is a decimal part. The type returned by Ceil () is still float, because the float value is usually larger than the integer. Ceil () Example: <?php
echo ceil (4.3); 5
Echo ceil (9.999);//
echo ceil ( -3.14);//-3
?> See the difference between the two functions. We'll use them when paging. Page number calculation: $LASTPG =ceil ($totle/$DISPLAYPG); The last page, is also the total number of pages, with ceil more convenient.
$LASTPG = $lastpg? $LASTPG: 1;//No entries are displayed, the last page is 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 precision, you need to use the round function. three, round function: Grammar: Float round (float val [, int precision]) Description Returns the result of rounding Val according to the specified precision precision (number of digits after the decimal point). Precision can also be negative or 0 (the default value). 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
?>
intval-the variable into an integer form Variable to an integer type. Syntax: int intval (mixed var, int [base]); return value: Integer Function type: PHP system function
Content Description This function converts a variable to an integer type. The parameter base that can be omitted is the base of the transformation, and the default value is 10. The converted variable var can be an array or any type of variable other than the class.
Example Intval () <?php
echo intval (4.3);//4
Echo intval (4.6);//4
Note: intval If the character type is automatically converted to 0 as Intval (' abc '); Output Results 0 If it is Intval (' 5fd '); The output result is 5 Note: Intval cannot handle numbers larger than 9 digits |