php中的四舍五入函数代码(floor函数、ceil函数、round与intval)_PHP
floor函数和ceil函数互相搭配起来可以使php 处理的数据更加真实可靠。
一、先来看floor函数:
语法:
float floor ( float value )
说明:
返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。
floor() 例子 1
<?php echo floor(1.6); // will output "1" echo floor(-1.6); // will output "-2" ?>
floor() 例子 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)) ?>
输出:
0
0
5
5
-6
-6
二、ceil函数:
语法:
float ceil ( float value )
说明:
返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。
ceil() 例子:
<?php echo ceil(4.3); // 5 echo ceil(9.999); // 10 echo ceil(-3.14); // -3 ?>
看到这两个函数的区别了么。。
在分页时我们会常用到的
//页码计算:
$lastpg=ceil($totle/$displaypg); //最后页,也是总页数,用ceil就方便多了。 $lastpg=$lastpg ? $lastpg : 1; //没有显示条目,置最后页为1 $page=min($lastpg,$page); $prepg=$page-1; //上一页 $nextpg=($page==$lastpg ? 0 : $page+1); //下一页 $firstcount=($page-1)*$displaypg;
当然,如果需要制定精度就需要使用round函数了。
三、round函数:
语法:
float round ( float val [, int precision] )
说明:
返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。
round() 例子
<?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 ?>
四、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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Validator can be created by adding the following two lines in the controller.
