Home > Backend Development > PHP Tutorial > Rounding function code in PHP (floor function, ceil function, round and intval), ceilintval_PHP tutorial

Rounding function code in PHP (floor function, ceil function, round and intval), ceilintval_PHP tutorial

WBOY
Release: 2016-07-13 10:23:26
Original
980 people have browsed it

Rounding function code in php (floor function, ceil function, round and intval), ceilintval

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

<&#63;php 
 echo floor(1.6); // will output "1" 
 echo floor(-1.6); // will output "-2" 
&#63;>
Copy after login

floor() example 2

<&#63;php
echo(floor(0.60));
echo(floor(0.40));
echo(floor(5));
echo(floor(5.1));
echo(floor(-5.1));
echo(floor(-5.9))
&#63;>
Copy after login

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:

<&#63;php 
echo ceil(4.3);  // 5 
echo ceil(9.999); // 10 
echo ceil(-3.14); // -3 
&#63;>
Copy after login

See the difference between these two functions? .

We will often use it when paginating
//Page number calculation:

  $lastpg=ceil($totle/$displaypg); //最后页,也是总页数,用ceil就方便多了。
  $lastpg=$lastpg &#63; $lastpg : 1; //没有显示条目,置最后页为1 
  $page=min($lastpg,$page); 
  $prepg=$page-1; //上一页 
  $nextpg=($page==$lastpg &#63; 0 : $page+1); //下一页 
  $firstcount=($page-1)*$displaypg; 

Copy after login

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

<&#63;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 
&#63;>

Copy after login

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()

<&#63;php 
echo intval(4.3); //4 
echo intval(4.6); // 4 
&#63;> 
Copy after login

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

What is the difference between the ceil function and the intval function in php?

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
?>

What is the difference between the ceil function and the intval function in php?

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;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840628.htmlTechArticleRounding function code in php (floor function, ceil function, round and intval), ceilintval floor function and ceil function Together, they can make the data processed by PHP more authentic and reliable. ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template