search
Homephp教程php手册php取整函数ceil,floo,round的用法及介绍

php取整函数ceil,floo,round的用法及介绍

Jun 13, 2016 pm 12:47 PM
ceilphproundintroducevaluefunctionRoundingyesusageof

ceil  是向上进位得到一个值的函数;
floor 是舍掉小数位得到一个值的函数;
round 是用来四舍五入的函数

ceil
定义和用法:
ceil() 函数向上舍入为最接近的整数。

复制代码 代码如下:


ceil(x);


说明:
返回不小于 x 的下一个整数,x 如果有小数部分则进一位。
ceil() 返回的类型仍然是 float。

例子:

复制代码 代码如下:


    echo ceil(0.60);
    echo "
";
    echo ceil(0.40);
    echo "
";
    echo ceil(5);
    echo "
";
    echo ceil(5.1);
    echo "
";
    echo ceil(-5.1);
    echo "
";
    echo ceil(-5.9);
?>


输出:

复制代码 代码如下:


1
1
5
6
-5
-5


floor
定义和用法:
floor() 函数向下舍入为最接近的整数。

复制代码 代码如下:


floor(x);


说明:
返回不大于 x 的下一个整数,将 x 的小数部分舍去取整。
floor() 返回的类型仍然是 float。

例子:

复制代码 代码如下:


    echo(floor(0.60));  
    echo "
";
    echo(floor(0.40));
 echo "
";
    echo(floor(5));
 echo "
";
    echo "
";
    echo(floor(5.1));
 echo "
";
    echo(floor(-5.1));
 echo "
";
    echo(floor(-5.9))
?>


输出:

复制代码 代码如下:


0
0
5
5
-6
-6


round
定义和用法
round() 函数对浮点数进行四舍五入。

复制代码 代码如下:


round(x,prec);


其中
x(可选)    规定要舍入的数字。
prec(可选)  规定小数点后的位数。

说明:
返回将 x 根据指定精度 prec (十进制小数点后数字的数目)进行四舍五入的结果。
prec 也可以是负数或零(默认值)。

例子:

复制代码 代码如下:


echo round(12.345,-1);
echo "
";
echo round(12.345);
echo "
";
echo round(0.5);
echo "
";
echo round(0.4);
echo "
";
echo round(-0.5);
echo "
";
echo round(-0.4);
?>


输出:

复制代码 代码如下:


10
12
1
0
-1
-0

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software