search
Homephp教程php手册php中四舍五入取整函数详细介绍

php中四舍五入取整函数详细介绍

Jun 13, 2016 am 10:14 AM
phpintroducefunctionRoundingandroundingusarticleusedetailed

本文章中我们介绍了php中四个常用的取整和四舍五入函数ceil,floor,round,intval ,下面我们来详细介绍。

ceil — 进一法取整
说明
float ceil ( float value )
返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。
例子 1. ceil() 例子

 代码如下 复制代码
echo ceil(4.3); // 5
echo ceil(9.999); // 10
?>

在分页时我们会常用到的


//页码计算: 

 代码如下 复制代码
   $lastpg=ceil($totle/$displaypg); //最后页,也是总页数 
   $lastpg=$lastpg ? $lastpg : 1; //没有显示条目,置最后页为1 
   $page=min($lastpg,$page); 
   $prepg=$page-1; //上一页 
   $nextpg=($page==$lastpg ? 0 : $page+1); //下一页 
   $firstcount=($page-1)*$displaypg; 

floor — 舍去法取整
说明
float floor ( float value )
返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。
例子 1. floor() 例子

例子
在本例中,我们将对不同的数应用 floor() 函数:

 代码如下 复制代码

echo(floor(0.60));
echo(floor(0.40));
echo(floor(5));
echo(floor(5.1));
echo(floor(-5.1));
echo(floor(-5.9))
?>输出:

0
0
5
5
-6
-6

round — 对浮点数进行四舍五入
说明
float round ( float val [, int precision] )
返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。

注释:PHP 默认不能正确处理类似 "12,300.2" 的字符串。

注释:prec 参数是在 PHP 4 中被引入的。。
例子

 代码如下 复制代码

echo(round(0.60));
echo(round(0.50));
echo(round(0.49));
echo(round(-4.40));
echo(round(-4.60));
?>输出:

1
1
0
-4
-5

intval—对变数转成整数型态

变量转成整数类型。

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

返回值: 整数

函数种类: PHP 系统功能

 
 
内容说明


本函数可将变量转成整数类型。可省略的参数 base 是转换的基底,默认值为 10。转换的变量 var 可以为数组或类之外的任何类型变量。

例子intval()

 代码如下 复制代码
echo intval(4.3); //4
echo intval(4.6); // 4
?>

注:intval如果是字符型的会自动转换为0 如

intval('abc');

输出结果 0

如果是

intval('5fd');

输出结果是

5

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor