Rounding function code in php of floor function ceil function round and intval

  • 2021-07-09 07:28:47
  • OfStack

The combination of floor and ceil functions makes the data processed by php more authentic and reliable.

1. Look at the floor function first:

Syntax:

float floor ( float value )

Description:

Returns the next integer not greater than value, rounding off 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 not less than value, and value is entered by one digit if there is a decimal part. 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 paging
//Page number calculation:


  $lastpg=ceil($totle/$displaypg); // The last page, which is also the total number of pages , Use ceil It's much more convenient. 
  $lastpg=$lastpg ? $lastpg : 1; // No entry displayed, set last page to 1 
  $page=min($lastpg,$page); 
  $prepg=$page-1; // Upper 1 Page  
  $nextpg=($page==$lastpg ? 0 : $page+1); // Under 1 Page  
  $firstcount=($page-1)*$displaypg; 

Of course, if you need to set accuracy, you need to use round function.

3. The round function:

Syntax:

float round ( float val [, int precision] )

Description:

Returns the result of rounding val by 4 to 5 according to the specified precision precision (the number of 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 to integers

Variable to an integer type.

Syntax: int intval (mixed var, int [base]);

Return value: Integer

Function category: PHP system function

Content description


This function converts a variable to an integer type. The omitted parameter base is the base of the transformation, and the default value is 10. The converted variable var can be any type variable other than an array or a class.

Example intval ()


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

Note: intval will be automatically converted to 0 if it is character type, such as

intval('abc');

Output result 0

If it is

intval('5fd');

The output is

5


Related articles: