本文章中我们介绍了php中四个常用的取整和四舍五入函数ceil,floor,round,intval ,下面我们来详细介绍.
ceil — 进一法取整
说明
float ceil ( float value )
返回不小于 value 的下一个整数,value 如果有小数部分则进一位.ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大.
例子 1. ceil() 例子
实例代码如下:
- <?php
- 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() 函数:
实例代码如下:
- <?php
- 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 中被引入的..
实例代码如下:
- <?php
- 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()
实例代码如下:
- <?php
- echo intval(4.3); //4
- echo intval(4.6); // 4
- ?>
注:intval如果是字符型的会自动转换为0 如
intval('abc');
输出结果 0
如果是
intval('5fd');
输出结果是
5
波比源码 » php中四舍五入取整函数详细介绍
levofloxacin medication levofloxacin 500mg cost
cialis 20mg ohne rezept sildenafil 100mg ohne rezept sildenafil kaufen für männer
cost accutane 10mg order stromectol 12mg generic ivermectin tablets for sale walmart
buy buspar 5mg for sale cost dilantin order ditropan generic
zoloft cost Free viagra sample sildenafil drug
avlosulfon uk dapsone 100mg cost aceon without prescription
order generic provigil 100mg online canadian pharmacy legit ivermectin tablets order
order chloroquine 250mg sale order generic baricitinib cheap baricitinib 4mg
order itraconazole 100 mg online cheap buy tindamax tindamax order online
linezolid 600mg usa slot games online free poker online free
purchase metoprolol generic cheap metoprolol vardenafil cheap
vardenafil 10mg over the counter pregabalin for sale online buy medrol 4mg
priligy buy online cheap synthroid tablets cheap synthroid generic
order cozaar 50mg for sale order topiramate for sale buy topamax
baclofen 25mg sale buy ketorolac pill toradol 10mg pill
order imitrex 50mg online avodart cost cheap avodart 0.5mg
buy zantac generic buy mobic 15mg generic order celecoxib 100mg pill
real money casino free welcome spins no deposit real online casino
buy diflucan 100mg generic viagra 50mg usa viagra cost
cephalexin 500mg oral order cleocin pills erythromycin 250mg usa
purchase sildalis pills lamotrigine for sale lamotrigine 200mg uk
purchase prednisone generic order deltasone 10mg pills buy generic amoxicillin 250mg
world tavern poker online online card games buy modafinil generic
fildena 50mg generic order budesonide online cheap rhinocort buy online