Home Backend Development PHP Tutorial php中的四舍五入函数代码(floor函数、ceil函数、round与intval)_PHP

php中的四舍五入函数代码(floor函数、ceil函数、round与intval)_PHP

Jun 01, 2016 am 11:51 AM
php rounding

floor函数和ceil函数互相搭配起来可以使php 处理的数据更加真实可靠。

一、先来看floor函数:

语法:

float floor ( float value )

说明:

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

floor() 例子 1

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

floor() 例子 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

输出:
0
0
5
5
-6
-6

二、ceil函数:

语法:

float ceil ( float value )

说明:

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

ceil() 例子:

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

看到这两个函数的区别了么。。

在分页时我们会常用到的
//页码计算:

  $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

当然,如果需要制定精度就需要使用round函数了。

三、round函数:

语法:

float round ( float val [, int precision] )

说明:

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

round() 例子

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

四、intval—对变数转成整数型态

变量转成整数类型。

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

返回值: 整数

函数种类: PHP 系统功能

内容说明


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

例子intval()

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

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

intval('abc');

输出结果 0

如果是

intval('5fd');

输出结果是

5

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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles