im钱包安卓版下载
数字资产服务平台

im钱包安卓版下载是全球著名的数字资产交易平台之一,主要面向全球用户提供比特币、莱特币、以太币等数字资产的币币和衍生品交易服务。

imtoken钱包安卓版下载|php四舍五入

时间:2024-03-07 20:32:04

php四舍五入函数(floor、ceil、round与intval) - wuling129 - 博客园

php四舍五入函数(floor、ceil、round与intval) - wuling129 - 博客园

会员

周边

新闻

博问

AI培训

云市场

所有博客

当前博客

我的博客

我的园子

账号设置

简洁模式 ...

退出登录

注册

登录

wuling129

博客园

首页

新随笔

联系

订阅

管理

php四舍五入函数(floor、ceil、round与intval)

原文链接:php四舍五入函数(floor、ceil、round与intval)

 

PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。

本文讲述了在php 中处理浮点数时经常要需要用的四舍五入函数。在php 中有两个函数适用于这种情况:floor函数、ceil函数和round函数

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

floor:舍去取整

ceil:取整,和floor功能相反

round:根据参数选择精度(这是真正的四舍五入)

intval:获取变量的整数值,如果参数是字符串,则反回0

一、先来看floor函数:

语法:

float floor ( float value )

说明:

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

floor() 例子 1

echo floor(1.6); // will output "1"

echo floor(-1.6); // will output "-2"

?>

floor() 例子 2

echo(floor(0.60));

echo(floor(0.40));

echo(floor(5));

echo(floor(5.1));

echo(floor(-5.1));

echo(floor(-5.9))

?>

输出:0055-6-6

二、ceil函数:

语法:

float ceil ( float value )

说明:

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

ceil() 例子:

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

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()

echo intval(4.3); //4

echo intval(4.6); // 4

?>

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

intval('abc');

输出结果 0

如果是

intval('5fd');

输出结果是

5

注:intval不能处理大于9位的数字

posted @

2015-12-31 14:03 

wuling129 

阅读(47415) 

评论(0) 

编辑 

收藏 

举报

会员力量,点亮园子希望

刷新页面返回顶部

公告

Copyright © 2024 wuling129

Powered by .NET 8.0 on Kubernetes

PHP 四舍五入实现方法 | 菜鸟教程

PHP 四舍五入实现方法 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

首页

笔记首页

Android

ES6 教程

排序算法

Hadoop

Zookeeper

Verilog

编程技术

程序员人生

首页

Android

ES6

逗乐

Search

PHP 四舍五入实现方法 分类 编程技术

PHP 实现四舍五入的函数为 round() ,语法格式如下:

float round ( float val [, int precision] )

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

实例

四舍五入数字到两位小数、设置负数:

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

?>

除了使用 round() 方法外,我们还可以使用 sprintf() 方法来实现四舍五入:

$num=0.0215489;

echo sprintf("%.3f", $num); // 0.022

$num2 = 123213.066666;

echo sprintf("%.2f", $num2); // 123213.07

?>

参考文章

PHP round() 函数

PHP sprintf() 函数

← Java 设计模式之组合模式及应用场景

JavaScript 中精度问题以及解决方案 →

点我分享笔记

取消

分享笔记

昵称昵称 (必填)

邮箱邮箱 (必填)

引用地址引用地址

教程列表

ADO 教程

Ajax 教程

Android 教程

Angular2 教程

AngularJS 教程

AppML 教程

ASP 教程

ASP.NET 教程

Bootstrap 教程

Bootstrap4 教程

Bootstrap5 教程

C 教程

C# 教程

C++ 教程

Chart.js 教程

CSS 参考手册

CSS 教程

CSS3 教程

Django 教程

Docker 教程

DTD 教程

ECharts 教程

Eclipse 教程

FastAPI 教程

Firebug 教程

Font Awesome 图标

Foundation 教程

Git 教程

Go 语言教程

Google 地图 API 教程

Highcharts 教程

HTML DOM 教程

HTML 参考手册

HTML 字符集

HTML 教程

HTTP 教程

ionic 教程

iOS 教程

Java 教程

JavaScript 参考手册

Javascript 教程

jQuery EasyUI 教程

jQuery Mobile 教程

jQuery UI 教程

jQuery 教程

JSON 教程

JSP 教程

Julia 教程

Kotlin 教程

Linux 教程

Lua 教程

Markdown 教程

Matplotlib 教程

Maven 教程

Memcached 教程

MongoDB 教程

MySQL 教程

Node.js 教程

NumPy 教程

Pandas 教程

Perl 教程

PHP 教程

PostgreSQL 教程

Python 3 教程

Python 基础教程

Python 量化交易

R 教程

RDF 教程

React 教程

Redis 教程

RSS 教程

Ruby 教程

Rust 教程

Sass 教程

Scala 教程

SciPy 教程

Servlet 教程

SOAP 教程

SQL 教程

SQLite 教程

SVG 教程

SVN 教程

Swift 教程

TCP/IP 教程

TypeScript 教程

VBScript 教程

Vue.js 教程

Vue3 教程

W3C 教程

Web Service 教程

WSDL 教程

XLink 教程

XML DOM 教程

XML Schema 教程

XML 教程

XPath 教程

XQuery 教程

XSLFO 教程

XSLT 教程

数据结构

正则表达式

测验

浏览器

网站品质

网站建设指南

网站服务器教程

设计模式

在线实例

·HTML 实例

·CSS 实例

·JavaScript 实例

·Ajax 实例

·jQuery 实例

·XML 实例

·Java 实例

字符集&工具

· HTML 字符集设置

· HTML ASCII 字符集

· JS 混淆/加密

· PNG/JPEG 图片压缩

· HTML 拾色器

· JSON 格式化工具

· 随机数生成器

最新更新

·

Rust 宏

·

Seaborn 教程

·

Pandas 相关性分析

·

31.2k star, 免...

·

Dev Home —...

·

免费开源的 AI ...

·

11.2k star, 免...

站点信息

·

意见反馈

·

免责声明

·

关于我们

·

文章归档

关注微信

Copyright © 2013-2024 菜鸟教程 

runoob.com All Rights Reserved. 备案号:闽ICP备15012807号-1

微信关注

PHP取整。四舍五入取整,进一法、舍去法取整_php四舍五入直到整数-CSDN博客

>

PHP取整。四舍五入取整,进一法、舍去法取整_php四舍五入直到整数-CSDN博客

PHP取整。四舍五入取整,进一法、舍去法取整

最新推荐文章于 2021-04-14 21:04:40 发布

W_Kenneth

最新推荐文章于 2021-04-14 21:04:40 发布

阅读量952

收藏

2

点赞数

分类专栏:

PHP

文章标签:

php

原文链接:http://blog.csdn.net/churk2012/article/details/51424588

版权

PHP

专栏收录该内容

9 篇文章

0 订阅

订阅专栏

PHP取整数函数常用的四种方法:

1.直接取整,舍弃小数,保留整数:intval(); 2.四舍五入取整:round(); 3.向上取整,有小数就加1:ceil(); 4.向下取整:floor()。

一、intval—对变数转成整数型态 intval如果是字符型的会自动转换为0。

intval(3.14159); // 3

intval(3.64159); // 3

intval('ruesin'); //0

二、四舍五入:round()

根据参数2指定精度将参数1进行四舍五入。参数2可以是负数或零(默认值)。

round(3.14159); // 3

round(3.64159); // 4

round(3.64159, 0); // 4

round(3.64159, 2); // 3.64

round(5.64159, 3); // 3.642

round(364159, -2); // 364200

三、向上取整,有小数就加1:ceil()

返回不小于 value 的下一个整数,value 如果有小数部分则进一位。

这个方法,在我们写分页类计算页数时经常会用到。

ceil(3.14159); // 4

ceil(3.64159); // 4

四、向下取整:floor()

返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。

floor(3.14159); // 3

floor(3.64159); // 3

转载自:http://blog.csdn.net/churk2012/article/details/51424588

优惠劵

W_Kenneth

关注

关注

0

点赞

2

收藏

觉得还不错?

一键收藏

知道了

0

评论

PHP取整。四舍五入取整,进一法、舍去法取整

PHP取整数函数常用的四种方法:1.直接取整,舍弃小数,保留整数:intval();2.四舍五入取整:round();3.向上取整,有小数就加1:ceil();4.向下取整:floor()。一、intval—对变数转成整数型态intval如果是字符型的会自动转换为0。intval(3.14159); // 3intval(3.64159); // 3intval('ruesin'); //0二、四舍五入:round()根据参数2指定精度将参数1进行四舍五入。参数2可以是

复制链接

扫一扫

专栏目录

PHP保留两位小数并且四舍五入及不四舍五入的方法

12-19

php保留两位小数并且四舍五入 复制代码 代码如下: $num = 123213.666666; echo sprintf(“%.2f”, $num); php保留两位小数并且不四舍五入 复制代码 代码如下: $num = 123213.666666; echo sprintf(“%.2f”,substr(sprintf(“%.3f”, $num), 0, -2)); php进一法取整 复制代码 代码如下: echo ceil(4.3); // 5 echo ceil(9.999); // 10 php舍去法,取整数 复制代码 代码如下: echo floor(4.3); // 4 ec

php实现进一法,php进一法取整、四舍五入取整、忽略小数等的取整数方法大全

weixin_42303078的博客

03-09

791

PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intvalPHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval一、ceil — 进一法取整说明float ...

参与评论

您还未登录,请先

登录

后发表或查看评论

php中除法进一的,php 除法取整

weixin_42575109的博客

04-09

464

如果我们使用" / "操作符进行除法运算时,如果遇到无法除尽的情况,会得到小数值。如果我只希望得到整数部分,怎么办呢?1.round — 对浮点数进行四舍五入float round ( float $val [, int $precision ] )返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。//E...

php取整到第一位,php取整的几种方式,四舍五入,舍去法取整,进一法取整

weixin_42157567的博客

04-14

353

php取整的几种方式,四舍五入,舍去法取整,进一法取整方式一:round 对浮点数进行四舍五入语法:float round ( float val [, int precision] )echo round(3.4); //echo round(3.5); //echo round(3.6); //echo round(3.6, 0); //echo round(1.95583, 2); // 1...

PHP取整,四舍五入取整、向上取整、向下取整、小数截取。

daiyan_csdn的博客

04-17

1334

PHP取整数函数常用的四种方法:

1.直接取整,舍弃小数,保留整数:intval();2.四舍五入取整:round();3.向上取整,有小数就加1:ceil();4.向下取整:floor()。

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

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

二、四舍五入:round()

根据参数2指定精度将参数1进行四舍五入。参数2可以是负数或零(默认值)。

三、向上

【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全

lz0426001的专栏

01-14

1万+

PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval

PHP取整数函数常用的四种方法,下面收集了四个函数;

经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval

一、cei

php四舍五入法, 进一取整法,舍去取整法

Robote Blog

07-03

3584

round() 四舍五入法

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);

PHP取整函数:ceil,floor,round,intval的区别详细解析

ak47147258369的博客

12-09

163

floor -- 舍去法取整说明float floor ( float value )

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

PHP取整函数例子 1. floor() 例子

复制代码 代码如下:

< ?php echo floor...

php实现进一法,PHP 进一法取整、四舍五入取整、忽略小数等的取整

weixin_29214691的博客

03-09

296

PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intvalPHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval一、ceil — 进一法取整说明float ...

php对数值取整的主要方法

jun524的博客

11-12

1363

php对数值取整的主要方法:

1.ceil 2.floor 3.round 4.intval

1.ceil 进一取整法:

&amp;lt;?php

echo ceil(4.3); //5

echo ceil(5.9); //6

echo ceil(9.99); //10

echo ceil(5.0); //5

注:返回大于等于它本身的最小整...

进一法 php,【php】进一法取整、四舍五入取整、忽略小数等的取整数方法大全...

weixin_42514750的博客

03-12

288

PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,,,PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,,,一、ceil进一法取整说明()返回不小于的下一个整数,value如果有小数部分则进一位。ceil()返回的类...

ceil — 进一法取整 4.1 = 5 5.9 = 6 取小数点两位

大任的网络专栏

04-10

1946

ceil — 进一法取整

float ceil ( float $value )

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

Example #1 ceil() 例子

echo ceil(4.3);    // 5

js 进一法取正、四舍五入法取正、舍去法取正

晓风残月

05-21

2万+

//仅取整数部分

Math.floor()

//小数进一

Math.ceil()

//四舍五入

Math.round()/*因为js里没有对小数进行精确的函数,想精确到小数后多少位,并四舍五入,还需要自己动手做一个:)

function round(v,e)

{

var t=1;

for(;e>0;t*=10,e--);

for(;e

return Math.

python 取整法(进一取值)

li_qingfeng的博客

05-16

1万+

import math

a = 3 / 2

print(a,type(a))

a = math.ceil(a)

print(a,type(a))

输出结果:

php中小数取整_PHP进一取整、四舍五入、保留两位小数,等等数字处理方法大全...

weixin_36254564的博客

03-09

376

这篇文章介绍的内容是关于PHP进一取整、四舍五入、保留两位小数,等等数字处理方法大全 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval一、ceil — 进一法取整说明float ceil ( float va...

PHP语法基础篇——除法取整和取余数

热门推荐

CAO的专栏

04-10

5万+

1. ceil — 进一取整函数

函数详解

float ceil ( float value )

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

示例:

1

2

3

echo ceil(4.333); // 5

?>

intval0.57100 php_PHP进一取整、四舍五入、保留两位小数,等等数字处理方法大全...

weixin_36026999的博客

02-01

97

PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intvalPHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval一、ceil — 进一法取整说明float ...

PHP---保留小数并且(不)四舍五入

记忆、深处

01-07

1万+

保留两位小数不进行四舍五入

$num=3.149;

$new_num=floor($num*100)/100;

echo $new_num;//结果3.14

保留两位小数,四舍五入

$num = 3.14999;

echo sprintf("%.2f", $num); //结果3.15

$n = 3.14899;

echo round($n,2);//结果3.15

拓展

ceil()//...

Kotlin Double 四舍五入 取整

最新发布

07-14

要将 Double 四舍五入并取整,你可以使用 Kotlin 的 `round()` 函数。下面是一个示例代码: ```kotlin fun main() { val number = 3.1415926 val roundedNumber = number.round() println(roundedNumber) // ...

“相关推荐”对你有帮助么?

非常没帮助

没帮助

一般

有帮助

非常有帮助

提交

W_Kenneth

CSDN认证博客专家

CSDN认证企业博客

码龄5年

暂无认证

28

原创

11万+

周排名

140万+

总排名

12万+

访问

等级

506

积分

8

粉丝

40

获赞

10

评论

164

收藏

私信

关注

热门文章

mysql,分组(group by)与排序(order by)同时使用

19382

python,生成随机数的几种方法

18223

进制转换的五种方法

12252

银行卡开户银行、名称缩写等信息查询

8761

Mysql四种常见数据库引擎

4831

分类专栏

windows

2篇

PHP

9篇

openssl

1篇

非对称加密

1篇

Git

8篇

python

3篇

PHP集成开发环境工具

1篇

yii2

2篇

phpstorm

3篇

memcached

数据库

6篇

mysql

11篇

composer

1篇

最新评论

python,生成随机数的几种方法

怎么吃不饱382:

你好,请问在(0,1)随机生成100个实数并且让它们的和为1怎么实现呢?

python,生成随机数的几种方法

代码小狗:

实数就uniform

python,生成随机数的几种方法

代码小狗:

random.randint(a, b)

Mysql四种常见数据库引擎

米达麦呀:

只有InnoDB支持事务

将Sublime Text 添加到鼠标右键菜单的教程方法

丿沫璃丨:

能打开 但是是新建文件是怎么回事

您愿意向朋友推荐“博客详情页”吗?

强烈不推荐

不推荐

一般般

推荐

强烈推荐

提交

最新文章

Mysql四种常见数据库引擎

ping命令判断目标主机操作系统

服务器并发量与那些因素有关

2022年4篇

2021年19篇

2020年45篇

目录

目录

分类专栏

windows

2篇

PHP

9篇

openssl

1篇

非对称加密

1篇

Git

8篇

python

3篇

PHP集成开发环境工具

1篇

yii2

2篇

phpstorm

3篇

memcached

数据库

6篇

mysql

11篇

composer

1篇

目录

评论

被折叠的  条评论

为什么被折叠?

到【灌水乐园】发言

查看更多评论

添加红包

祝福语

请填写红包祝福语或标题

红包数量

红包个数最小为10个

红包总金额

红包金额最低5元

余额支付

当前余额3.43元

前往充值 >

需支付:10.00元

取消

确定

下一步

知道了

成就一亿技术人!

领取后你会自动成为博主和红包主的粉丝

规则

hope_wisdom 发出的红包

实付元

使用余额支付

点击重新获取

扫码支付

钱包余额

0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。 2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值

PHP round() 函数 | 菜鸟教程

PHP round() 函数 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

首页

HTML

CSS

JavaScript

Vue

Bootstrap

NodeJS

Python3

Python2

Java

C

C++

C#

Go

SQL

Linux

jQuery

本地书签

首页

HTML

CSS

JS

本地书签

Search

Python3 教程

Python2 教程

Vue3 教程

vue2 教程

Bootstrap3 教程

Bootstrap4 教程

Bootstrap5 教程

Bootstrap2 教程

PHP 教程

PHP 教程

PHP 简介

PHP 安装

PHP 语法

PHP 变量

PHP echo/print

PHP EOF(heredoc)

PHP 数据类型

PHP 类型比较

PHP 常量

PHP 字符串

PHP 运算符

PHP If...Else

PHP Switch

PHP 数组

PHP 数组排序

PHP 超级全局变量

PHP While 循环

PHP For 循环

PHP 函数

PHP 魔术常量

PHP 命名空间

PHP 面向对象

PHP 测验

PHP 表单

PHP 表单

PHP 表单验证

PHP 表单 - 必需字段

PHP 表单 - 验证邮件和URL

PHP 完整表单实例

PHP $_GET 变量

PHP $_POST 变量

PHP 高级教程

PHP 多维数组

PHP 日期

PHP 包含

PHP 文件

PHP 文件上传

PHP Cookie

PHP Session

PHP E-mail

PHP 安全 E-mail

PHP Error

PHP Exception

PHP 过滤器

PHP 高级过滤器

PHP JSON

PHP 7 新特性

PHP 7 新特性

PHP 数据库

PHP MySQL 简介

PHP MySQL 连接

PHP MySQL 创建数据库

PHP MySQL 创建数据表

PHP MySQL 插入数据

PHP MySQL 插入多条数据

PHP MySQL 预处理语句

PHP MySQL 读取数据

PHP MySQL Where

PHP MySQL Order By

PHP MySQL Update

PHP MySQL Delete

PHP ODBC

PHP XML

XML Expat Parser

XML DOM

XML SimpleXML

PHP 与 AJAX

AJAX 简介

AJAX PHP

AJAX 数据库

AJAX XML

AJAX 实时搜索

AJAX RSS Reader

AJAX 投票

PHP 参考手册

PHP Array

PHP Calendar

PHP cURL

PHP Date

PHP Directory

PHP Error

PHP Filesystem

PHP Filter

PHP FTP

PHP HTTP

PHP Libxml

PHP Mail

PHP Math

PHP Misc

PHP MySQLi

PHP PDO

PHP SimpleXML

PHP String

PHP XML

PHP Zip

PHP Timezones

PHP 图像处理

PHP RESTful

PHP PCRE

PHP 可用的函数

PHP Composer

PHP rand() 函数

PHP sin() 函数

PHP round() 函数

PHP Math 参考手册

实例

对浮点数进行四舍五入:

echo(round(0.60) . "
");

echo(round(0.50) . "
");

echo(round(0.49) . "
");

echo(round(-4.40) . "
");

echo(round(-4.60));

?>

运行实例 »

定义和用法

round() 函数对浮点数进行四舍五入。

提示:如需向上舍入为最接近的整数,请查看 ceil() 函数。

提示:如需向下舍入为最接近的整数,请查看 floor() 函数。

语法

round(number,precision,mode);

参数

描述

number

必需。规定要舍入的值。

precision

可选。规定小数点后的尾数。默认是 0,也可以为负数。

mode

可选。规定表示舍入模式的常量:

PHP_ROUND_HALF_UP - 默认。遇到 .5 的情况时向上舍入 number 到 precision 小数位。舍入 1.5 到 2,舍入 -1.5 到 -2。

PHP_ROUND_HALF_DOWN - 遇到 .5 的情况时向下舍入 number 到 precision 小数位。舍入 1.5 到 1,舍入 -1.5 到 -1。

PHP_ROUND_HALF_EVEN - 遇到 .5 的情况时取下一个偶数值舍入 number 到 precision 小数位。

PHP_ROUND_HALF_ODD - 遇到 .5 的情况时取下一个奇数值舍入 number 到 precision 小数位。

技术细节

返回值:

舍入后的值。

返回类型:

Float

PHP 版本:

4+

PHP 更新日志:

PHP 5.3:新增 mode 参数。

更多实例

实例 1

四舍五入数字到两位小数、设置负数:

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

?>

实例 2

使用常量对数字进行四舍五入:

echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10

echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9

echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10

echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9

echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9

echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8

echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8

echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9

?>

PHP Math 参考手册

PHP rand() 函数

PHP sin() 函数

点我分享笔记

取消

分享笔记

昵称昵称 (必填)

邮箱邮箱 (必填)

引用地址引用地址

分类导航

HTML / CSSHTML 教程HTML5 教程CSS 教程CSS3 教程Bootstrap3 教程Bootstrap4 教程Bootstrap5 教程Font Awesome 教程Foundation 教程 JavaScriptJavaScript 教程HTML DOM 教程jQuery 教程AngularJS 教程AngularJS2 教程Vue.js 教程Vue3 教程React 教程TypeScript 教程jQuery UI 教程jQuery EasyUI 教程Node.js 教程AJAX 教程JSON 教程Echarts 教程Chart.js 教程Highcharts 教程Google 地图 教程 服务端Python 教程Python2.x 教程Linux 教程Docker 教程Ruby 教程Java 教程C 教程C++ 教程Perl 教程Servlet 教程JSP 教程Lua 教程Rust 教程Scala 教程Go 教程PHP 教程数据结构与算法Django 教程FastAPI 教程Zookeeper 教程设计模式正则表达式Maven 教程Verilog 教程ASP 教程AppML 教程VBScript 教程 数据库SQL 教程MySQL 教程PostgreSQL 教程SQLite 教程MongoDB 教程Redis 教程Memcached 教程 数据分析Python 教程NumPy 教程Pandas 教程Matplotlib 教程Scipy 教程R 教程Julia 教程 移动端Android 教程Swift 教程jQuery Mobile 教程ionic 教程Kotlin 教程 XML 教程XML 教程DTD 教程XML DOM 教程XSLT 教程XPath 教程XQuery 教程XLink 教程XPointer 教程XML Schema 教程XSL-FO 教程SVG 教程 ASP.NETASP.NET 教程C# 教程Web Pages 教程Razor 教程MVC 教程Web Forms 教程 Web ServiceWeb Service 教程WSDL 教程SOAP 教程RSS 教程RDF 教程 开发工具Eclipse 教程Git 教程Svn 教程Markdown 教程 网站建设HTTP 教程网站建设指南浏览器信息网站主机教程TCP/IP 教程W3C 教程网站品质

Advertisement

反馈/建议

在线实例

·HTML 实例

·CSS 实例

·JavaScript 实例

·Ajax 实例

·jQuery 实例

·XML 实例

·Java 实例

字符集&工具

· HTML 字符集设置

· HTML ASCII 字符集

· JS 混淆/加密

· PNG/JPEG 图片压缩

· HTML 拾色器

· JSON 格式化工具

· 随机数生成器

最新更新

·

Rust 宏

·

Seaborn 教程

·

Pandas 相关性分析

·

31.2k star, 免...

·

Dev Home —...

·

免费开源的 AI ...

·

11.2k star, 免...

站点信息

·

意见反馈

·

免责声明

·

关于我们

·

文章归档

关注微信

Copyright © 2013-2024 菜鸟教程 

runoob.com All Rights Reserved. 备案号:闽ICP备15012807号-1

微信关注

PHP实现数据进行四舍五入的4种方法_php四舍五入百位怎么写-CSDN博客

>

PHP实现数据进行四舍五入的4种方法_php四舍五入百位怎么写-CSDN博客

PHP实现数据进行四舍五入的4种方法

最新推荐文章于 2023-08-18 15:01:28 发布

ZhiHuaWei

最新推荐文章于 2023-08-18 15:01:28 发布

阅读量1k

收藏

点赞数

分类专栏:

PHP

文章标签:

PHP

四舍五入

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/Zhihua_W/article/details/52262721

版权

PHP

专栏收录该内容

68 篇文章

0 订阅

订阅专栏

在PHP开发中,有时候我们会遇到将数据进行四舍五入的运算情况,本博文分享了用PHP实现数据四舍五入的4种方法。

php实现数据四舍五入的4种方法,分别通过number_format()函数、round()函数和sprintf()格式化以及intval()函数输出的方法实现四舍五入。严格来说最后一种不是严格的四舍五入,最后一种仅是将数据的小数部分去掉,只保留整数部分,详细情况会在下面进行讨论。

1、number_format() 方法实现四舍五入

number_format() 函数通过千位分组来格式化数字。

//定义一个float型的变量

$number = 1234.5678;

//English Notation (defult)

$number_format_english = number_format($number);

//1,235

$number_format_english = number_format($number, 2, '.', '');

//1234.57

//French Notation

$number_format_francais = number_format($number, 2, ',', '');

//1234,57

$number_format_francais = number_format($number, 3, ',', '');

//1234,568

echo $number_format_english;

//1234.57

echo $number_format_francais;

//1234,568

2、round()方法实现四舍五入

round() 函数对浮点数进行四舍五入。

//定义一个float型的变量

$number = 1234.5678;

//不保留小数

echo round($number);

//1235

//保留两位小树

echo round($number,2);

//1234.57

echo "
";

$number = 12345678;

//在千分位进行四舍五入

echo round($number,-4);

//12350000

3、sprintf() 格式化输入实现四舍五入

字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。sprintf 是个变参函数。

//定义一个正整数

$n = 43951789;

//定义一个负整数

$u = -43951789;

// ASCII 65 is 'A'

$c = 65;

printf("%%b = '%b'\n", $n);

//%b = '10100111101010011010101101'

printf("%%c = '%c'\n", $c);

//%c = 'A'

printf("%%d = '%d'\n", $n);

//%d = '43951789'

printf("%%e = '%e'\n", $n);

//%e = '4.395179e+7'

printf("%%u = '%u'\n", $n);

//%u = '43951789'

printf("%%u = '%u'\n", $u);

//%u = '4251015507'

printf("%%f = '%f'\n", $n);

//%f = '43951789.000000'

printf("%%o = '%o'\n", $n);

//%o = '247523255'

printf("%%s = '%s'\n", $n);

//%s = '43951789'

printf("%%x = '%x'\n", $n);

//%x = '29ea6ad'

printf("%%X = '%X'\n", $n);

//%X = '29EA6AD'

printf("%%+d = '%+d'\n", $n);

//%+d = '+43951789'

printf("%%+d = '%+d'\n", $u);

//%+d = '-43951789'

4、intval()函数实现整数输出

此方法不是严格的四舍五入执行,其将数据的小数部分强制清除达到只输出整数部分的效果。

//定义一个浮点数

$number = 1234.5678;

$number_int = intval($number);

echo $number_int;

//1234

优惠劵

ZhiHuaWei

关注

关注

0

点赞

0

收藏

觉得还不错?

一键收藏

知道了

0

评论

PHP实现数据进行四舍五入的4种方法

在PHP开发中,有时候我们会遇到将数据进行四舍五入的运算情况,本博文分享了用PHP实现数据四舍五入的4种方法。php实现数据四舍五入的4种方法,分别通过number_format()函数、round()函数和sprintf()格式化以及intval()函数输出的方法实现四舍五入。严格来说最后一种不是严格的四舍五入,最后一种仅是将数据的小数部分去掉,只保留整数部分,详细情况会在下面进行讨论。

复制链接

扫一扫

专栏目录

PHP实现数据四舍五入的方法小结【4种方法】

01-02

在PHP开发中,有时候我们会遇到将数据进行四舍五入的运算情况,本文分享了用PHP实现数据四舍五入的4种方法。 php实现数据四舍五入的4种方法,分别通过number_format()函数、round()函数和sprintf()格式化以及intval...

PHP_保留两位小数并且四舍五入(可用于精度计算)_保留两位小数并且不四舍五入,

热门推荐

houxianyj的专栏

06-26

4万+

php保留两位小数并且四舍五入

参与评论

您还未登录,请先

登录

后发表或查看评论

php 数据处理之四舍五入取整实现4种方法

qq_32450471的博客

09-23

1533

2.向上取整,有小数就整数部分加1。1.丢弃小数部分,保留整数部分。,后来参数根保留小数位数即可。3, 对浮点数进行四舍五入。

PHP 四舍五入实现方法

最新发布

qq_32450471的博客

08-18

385

PHP 四舍五入实现方法1. PHP 实现四舍五入的函数为 round() ,语法格式如下:除了使用 round() 方法外,我们还可以使用 sprintf() 方法来实现四舍五入:

round()四舍五入php函数介绍

winkexin的博客

06-11

185

precision:可选。规定小数点后的尾数。默认是 0,也可以为负数。round()函数对浮点数进行四舍五入。规定表示舍入模式的常量。number:必需。

php 四舍五入

weixin_38155824的博客

06-21

538

php四舍五入

PHP四舍五入函数round和sprintf比较

椰子园

09-03

616

先来看一个例子

$a = 4.5449;

$b = 4.5500;

$c = 4.5501;

$d = 4.5549;

$e = 4.5550;

$f = 4.5551;

//保留1位小数对比:

echo round($a,1); // 4.5

echo sprintf('%.1f',$a); // 4.5

echo round($b,1); // 4.6

echo sprintf('%.1f',$b); // 4.5

echo round($c,1);

PHP实现数据四舍五入的4中方法

05-29

PHP实现数据四舍五入的4中方法 一、开发环境 1、环境搭建:Windows 8+Apache 2.4.18+MySQL 5.7.11+PHP 7.1.0 。 2、文本编辑器:Sublime Text3。 二、主要技术 本实验使用PHP 7下number_format()函数,round()函数...

php实现四舍五入的方法小结

01-20

php实现四舍五入的三种方法,分别通过number_format函数、round函数和sprintf格式化输出的方法实现四舍五入 1.number_format 方法实现四舍五入 $number = 1234.5678; $nombre_format_francais = number_format($...

php怎么四舍五入,PHP实现四舍五入的3种方法

weixin_28998761的博客

03-18

1819

在PHP开发中,有时候我们会遇到四舍五入的运算情况,本文分享了用PHP实现四舍五入的3种方法。php实现四舍五入的三种方法,分别通过number_format函数、round函数和sprintf格式化输出的方法实现四舍五入。1.number_format 方法实现四舍五入$number = 1234.5678;$nombre_format_francais = number_format($num...

PHP 四舍五入函数和进一函数

06-08

NULL

博文链接:https://onestopweb.iteye.com/blog/2332399

PHP取整,四舍五入取整、向上取整、向下取整、小数截取。

daiyan_csdn的博客

04-17

1334

PHP取整数函数常用的四种方法:

1.直接取整,舍弃小数,保留整数:intval();2.四舍五入取整:round();3.向上取整,有小数就加1:ceil();4.向下取整:floor()。

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

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

二、四舍五入:round()

根据参数2指定精度将参数1进行四舍五入。参数2可以是负数或零(默认值)。

三、向上

PHP数值的保留2位小数和四舍五入

akazhuk的专栏

02-20

4527

$price = 123;

1、return round($price,2);

2、return number_format($price, 2, '.', '');第一种方法是四舍五入并保留2位小数,如果price=123.2233,会变成123.22。但如果price=123,数值会变成123,后面不会有.00。

第二种方法是保留2位小数并四舍五入,虽然跟第一种方法看似一样,但使用第二种方法

PHP四舍五入精确小数位及取整

华章酱的博客

08-29

988

经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval

进一法取整、四舍五入取整、忽略小数等的取整数方法大全

PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval 

PHP取整数函数常用的四种方法,...

php 保留两位小数 四舍五入的函数,php保存小数点后两位并且四舍五入的方法

weixin_36469247的博客

03-10

476

在PHP开发中我们经常会遇到小数四舍五入的问题,之前小编就分享了几篇关于PHP四舍五入的几种方法,现在我们接着分析php保存小数点后两位并且四舍五入的方法。php保留两位小数并且四舍五入代码如下:$num = 123213.666666;echo sprintf("%.2f", $num);php保留两位小数并且不四舍五入代码如下:$num = 123213.666666;echo sprintf...

PHP取整,四舍五入取整、向上取整、向下取整、小数截取

firstcode666的博客

12-15

994

PHP取整数函数常用的四种方法:

1.直接取整,舍弃小数,保留整数:intval();

2.四舍五入取整:round();

3.向上取整,有小数就加1:ceil();

4.向下取整:floor()。

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

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

intval(3.14159); // 3

intval(3.64159); // 3

intval('ruesin'); //0

1

2

3

二、四舍五入:round()

根据参数2指定精度将参数1进行四舍...

sprintf() 格式化数字,小数位数,四舍五入

opfan的博客

12-15

2万+

//生成4位数,不足前面补0

$var=sprintf("%04d", 2);

echo $var;//结果为0002

echo date('Y_m_d', time()).'_'.sprintf('d', rand(0,99));

?>

 

 

 

 

 

 

sprintf()函数

php怎么四舍五入,php 四舍五入的三种实现方法

weixin_39940788的博客

03-18

231

这篇文章主要为大家详细介绍了php 四舍五入的三种实现方法,具有一定的参考价值,可以用来参考一下。对php 四舍五入的三种方法感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧!php 四舍五入的三种方法,分别通过number_format函数、round函数和sprintf格式化输出的方法实现四舍五入/*** php 四舍五入的三种方法** @param* @arrange 512-笔记...

php 四舍五入,php四舍五入 js四舍五入方法 | 帮助信息-动天数据

weixin_35725310的博客

03-09

167

php四舍五入 js四舍五入方法作者:dthost |时间:2017-12-16 |分类:未分类 |6,286 次阅读四舍五入是一种精确度的计数保留法,与其他方法本质相同。特殊之处在于,采用四舍五入,能使被保留部分的与实际值差值不超过最后一位数量级的二分之一:假如0~9等概率出现的话,对大量的被保留数据,这种保留法的误差总和是最小的,因此四舍五入法也是最基本的保留法。、例子一:例如π被四舍五入,保...

PHP7.2版本 不四舍五入保留两位

06-08

默认情况下,`number_format` 函数会对数字进行四舍五入,但是我们可以通过设置第四个参数来控制保留小数位数的方式。例如,如果我们想保留两位小数并且不进行四舍五入,可以使用以下代码: ```php $number = 123....

“相关推荐”对你有帮助么?

非常没帮助

没帮助

一般

有帮助

非常有帮助

提交

ZhiHuaWei

CSDN认证博客专家

CSDN认证企业博客

码龄8年

暂无认证

168

原创

1万+

周排名

218万+

总排名

102万+

访问

等级

9185

积分

747

粉丝

570

获赞

200

评论

1876

收藏

私信

关注

热门文章

如何在url地址栏中直接写数组参数进行传递

71666

Js中去除数组中重复元素的4种方法

57261

如何使用百度地图API自动获取地址和经纬度

43153

ThinkPHP实现定时执行任务的两种方法

41088

Python实现连接操作MySql数据库

33578

分类专栏

Java

2篇

Python爬虫

26篇

PHP开发小技巧

26篇

CodeIgniter源码解析

20篇

详解PHP设计模式

5篇

PHP

68篇

HTML&JS

15篇

CodeIgniter

28篇

Python

32篇

ThinkPHP

6篇

Git&SVN

12篇

Memcached&Redis

3篇

MySQL

3篇

API

1篇

Composer

3篇

Alipay&Wxpay

8篇

Technology

3篇

Mui&H5+

1篇

最新评论

Python爬虫4.4 — selenium高级用法教程

w1047667241:

[code=plain]

disable-infobars参数已失效。改为 设置 options方式

ChromeOptions options = new ChromeOptions()

.addArguments("--disable-infobars") // no longer works any more

// here is the solution

.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"}) ;

// put it as args

ChromeDriver webDriver = new ChromeDriver(options);

// OK,enjoy

webDriver.get("https://www.google.com");

[/code]

基于PHP和JS的AES相互加密解密方法详解(CryptoJS)

暂时先用这个名字:

嗯,看到了,谢谢。

基于PHP和JS的AES相互加密解密方法详解(CryptoJS)

ZhiHuaWei:

这个之前有更新过,您可以参考下:https://blog.csdn.net/Zhihua_W/article/details/112396537

基于PHP和JS的AES相互加密解密方法详解(CryptoJS)

暂时先用这个名字:

非常感谢!成功了。现在php8加密解密已经换成了openssl_encrypt(),所以在php部分需改为

[code=php]

//最新

openssl_decrypt($data, 'AES-128-CBC', $key,2, $iv);

//原来

mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($data), MCRYPT_MODE_CBC, $iv);

[/code]

Js中去除数组中重复元素的4种方法

m0_74025242:

this是被点击的标签对象,你是咋用来去随机数的,按照你的用法,那个【0】就是多余的

您愿意向朋友推荐“博客详情页”吗?

强烈不推荐

不推荐

一般般

推荐

强烈推荐

提交

最新文章

SpringBoot常用注解总结

JDBC技术详解

Git版本回退的两种方式及回退方式推荐

2021年5篇

2020年10篇

2019年30篇

2018年38篇

2017年34篇

2016年58篇

目录

目录

分类专栏

Java

2篇

Python爬虫

26篇

PHP开发小技巧

26篇

CodeIgniter源码解析

20篇

详解PHP设计模式

5篇

PHP

68篇

HTML&JS

15篇

CodeIgniter

28篇

Python

32篇

ThinkPHP

6篇

Git&SVN

12篇

Memcached&Redis

3篇

MySQL

3篇

API

1篇

Composer

3篇

Alipay&Wxpay

8篇

Technology

3篇

Mui&H5+

1篇

目录

评论

被折叠的  条评论

为什么被折叠?

到【灌水乐园】发言

查看更多评论

添加红包

祝福语

请填写红包祝福语或标题

红包数量

红包个数最小为10个

红包总金额

红包金额最低5元

余额支付

当前余额3.43元

前往充值 >

需支付:10.00元

取消

确定

下一步

知道了

成就一亿技术人!

领取后你会自动成为博主和红包主的粉丝

规则

hope_wisdom 发出的红包

实付元

使用余额支付

点击重新获取

扫码支付

钱包余额

0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。 2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值

PHP: round - Manual

PHP: round - Manual

Downloads

Documentation

Get Involved

Help

Getting Started

Introduction

A simple tutorial

Language Reference

Basic syntax

Types

Variables

Constants

Expressions

Operators

Control Structures

Functions

Classes and Objects

Namespaces

Enumerations

Errors

Exceptions

Fibers

Generators

Attributes

References Explained

Predefined Variables

Predefined Exceptions

Predefined Interfaces and Classes

Predefined Attributes

Context options and parameters

Supported Protocols and Wrappers

Security

Introduction

General considerations

Installed as CGI binary

Installed as an Apache module

Session Security

Filesystem Security

Database Security

Error Reporting

User Submitted Data

Hiding PHP

Keeping Current

Features

HTTP authentication with PHP

Cookies

Sessions

Dealing with XForms

Handling file uploads

Using remote files

Connection handling

Persistent Database Connections

Command line usage

Garbage Collection

DTrace Dynamic Tracing

Function Reference

Affecting PHP's Behaviour

Audio Formats Manipulation

Authentication Services

Command Line Specific Extensions

Compression and Archive Extensions

Cryptography Extensions

Database Extensions

Date and Time Related Extensions

File System Related Extensions

Human Language and Character Encoding Support

Image Processing and Generation

Mail Related Extensions

Mathematical Extensions

Non-Text MIME Output

Process Control Extensions

Other Basic Extensions

Other Services

Search Engine Extensions

Server Specific Extensions

Session Extensions

Text Processing

Variable and Type Related Extensions

Web Services

Windows Only Extensions

XML Manipulation

GUI Extensions

Keyboard Shortcuts?

This help

j

Next menu item

k

Previous menu item

g p

Previous man page

g n

Next man page

G

Scroll to bottom

g g

Scroll to top

g h

Goto homepage

g s

Goto search(current page)

/

Focus search box

sin »

« rad2deg

PHP 手册 函数参考 数学扩展 Math Math 函数

Change language:

English

German

Spanish

French

Italian

Japanese

Brazilian Portuguese

Russian

Turkish

Chinese (Simplified)

Other

Submit a Pull Request

Report a Bug

round

(PHP 4, PHP 5, PHP 7, PHP 8)round — 对浮点数进行四舍五入

说明

round(int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float

返回将 num 根据指定精度

precision(十进制小数点后数字的数目)进行四舍五入的结果。precision

也可以是负数或零(默认值)。

参数

num

要处理的值。

precision

可选的十进制小数点后数字的数目。

如果 precision 是正数,则 num

会四舍五入到小数点后 precision 位有效数字。

如果 precision 是负数,则 num

四舍五入到小数点前 precision 位有效数字。即 pow(10,

-$precision) 最接近的倍数,例如,precision

为 -1,num 可以四舍五入到十位,precision

为 -2,num 可以四舍五入到百位,等等。

mode

使用以下常量指定四舍五入发生的模式。

常量

说明

PHP_ROUND_HALF_UP

当 num 恰好处于中间时,将其向远离零的方向舍入,使 1.5 变为 2,-1.5 变为 -2。

PHP_ROUND_HALF_DOWN

当 num 恰好处于中间时,将其向靠近零的方向舍入,使 1.5 变为 1,-1.5 变为 -1。

PHP_ROUND_HALF_EVEN

将 num 四舍五入到最接近的偶数值,1.5 和 2.5 都变为 2。

PHP_ROUND_HALF_ODD

将 num 四舍五入到最接近的奇数值,1.5 变为 1,2.5 变为 3。

返回值

值是四舍五入到指定 precision 精度的 float。

更新日志

版本

说明

8.0.0

num 不再接受支持数字转换的内部对象。

示例

示例 #1 round() 示例

以上示例会输出:

float(3)

float(4)

float(4)

float(4)

float(5.05)

float(5.06)

float(300)

float(0)

float(700)

float(1000)

示例 #2 precision 如何影响 float

以上示例会输出:

float(135.79)

float(135.79)

float(135.8)

float(136)

float(140)

float(100)

float(0)

示例 #3 mode 示例

以上示例会输出:

Rounding modes with 9.5

float(10)

float(9)

float(10)

float(9)

Rounding modes with 8.5

float(9)

float(8)

float(8)

float(9)

示例 #4 带 precision 的 mode 示例

以上示例会输出:

Using PHP_ROUND_HALF_UP with 1 decimal digit precision

float(1.6)

float(-1.6)

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision

float(1.5)

float(-1.5)

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision

float(1.6)

float(-1.6)

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision

float(1.5)

float(-1.5)

参见

ceil() - 进一法取整

floor() - 舍去法取整

number_format() - 以千位分隔符方式格式化一个数字

+add a note

User Contributed Notes 32 notes

up

down

316

takingsides at gmail dot com ¶9 years ago

In my opinion this function lacks two flags:- PHP_ROUND_UP - Always round up.- PHP_ROUND_DOWN - Always round down.In accounting, it's often necessary to always round up, or down to a precision of thousandths.

up

down

33

depaula at unilogica dot com ¶7 years ago

As PHP doesn't have a a native number truncate function, this is my solution - a function that can be usefull if you need truncate instead round a number.truncate(-1.49999, 2); // returns -1.49 * truncate(.49999, 3); // returns 0.499 * * @param float $val Float number to be truncate * @param int f Number of precision * @return float */function truncate($val, $f="0"){ if(($p = strpos($val, '.')) !== false) { $val = floatval(substr($val, 0, $p + 1 + $f)); } return $val;}?>Originally posted in http://stackoverflow.com/a/12710283/1596489

up

down

2

Anonymous ¶7 years ago

Note that PHP 5.3 didn't just introduce $mode, it rewrote the rounding implementation completely to eliminate many kinds of rounding errors common to rounding floating point values.That's why round() gives you the correct result even when floor/ceil don't.For example, floor(0.285 * 100 + 0.5) VS round(0.285*100 + 0.5). First one gives 28, second one gives 29.More details here: https://wiki.php.net/rfc/rounding

up

down

29

slimusgm at gmail dot com ¶9 years ago

If you have negative zero and you need return positive number simple add +0:$number = -2.38419e-07;var_dump(round($number,1));//float(-0)var_dump(round($number,1) + 0);//float(0)

up

down

13

serg at kalachev dot ru ¶9 years ago

Excel-like ROUNDUP function:public static function round_up($value, $places) { $mult = pow(10, abs($places)); return $places < 0 ? ceil($value / $mult) * $mult : ceil($value * $mult) / $mult;}echo round_up(12345.23, 1); // 12345.3echo round_up(12345.23, 0); // 12346 echo round_up(12345.23, -1); // 12350 echo round_up(12345.23, -2); // 12400 echo round_up(12345.23, -3); // 13000 echo round_up(12345.23, -4); // 20000

up

down

7

Mojo urk ¶6 years ago

Solving round_down() problem: -----------------------------Use of fails in some cases, e.g. round_down(2.05, 2) gives incorrect 2.04.Here is a "string" solution (https://stackoverflow.com/a/26491492/1245149) of the problem (a negative precision is not covered):Solving round_up() problem:---------------------------Use of fails in some cases, e.g. round_up(2.22, 2) gives incorrect 2.23 (https://stackoverflow.com/a/8239620/1245149).Adapting the above round_down() "string" solution I have got this result (a negative precision is not covered):I don't know it is bulletproof, but at least it removes the above mentioned fail. I have done no binary-to-decimal-math-analysis but if `$floorValue + pow(10, 0 - $precision)` worksalways as expected then it should be ok.

up

down

21

djcox99 at googlemail dot com ¶10 years ago

I discovered that under some conditions you can get rounding errors with round when converting the number to a string afterwards.To fix this I swapped round() for number_format().Unfortunately i cant give an example (because the number cant be represented as a string !)essentially I had round(0.688888889,2);which would stay as 0.68888889 when printed as a string.But using number_format it correctly became 0.69.

up

down

10

craft at ckdevelop dot org ¶10 years ago

function mround($val, $f=2, $d=6){ return sprintf("%".$d.".".$f."f", $val);}echo mround(34.89999); //34.90

up

down

11

jongbumi at gmail dot com ¶7 years ago

PHP 5.3, 5.4, 5.5PHP 5.6PHP 7

up

down

18

esion99 at gmail dot com ¶9 years ago

Unexpected result or misunderstanding (php v5.5.9)

up

down

10

christian at deligant dot net ¶12 years ago

this function (as all mathematical operators) takes care of the setlocale setting, resulting in some weirdness when using the result where the english math notation is expected, as the printout of the result in a width: style attribute!

up

down

19

twan at ecreation dot nl ¶23 years ago

If you'd only want to round for displaying variables (not for calculating on the rounded result) then you should use printf with the float: This returns: 3.40 .

up

down

17

Anonymous ¶13 years ago

Here is function that rounds to a specified increment, but always up. I had to use it for price adjustment that always went up to $5 increments.

up

down

8

michaeldnelson dot mdn at gmail dot com ¶14 years ago

This function will let you round to an arbitrary non-zero number. Zero of course causes a division by zero.

up

down

4

greghenle at gmail dot com ¶7 years ago

/** * Round to first significant digit * +N to +infinity * -N to -infinity * */function round1stSignificant ( $N ) { if ( $N === 0 ) { return 0; } $x = floor ( log10 ( abs( $N ) ) ); return ( $N > 0 ) ? ceil( $N * pow ( 10, $x * -1 ) ) * pow( 10, $x ) : floor( $N * pow ( 10, $x * -1 ) ) * pow( 10, $x );}echo round1stSignificant( 39144818 ) . PHP_EOL;echo round1stSignificant( 124818 ) . PHP_EOL;echo round1stSignificant( 0.07468 ) . PHP_EOL;echo round1stSignificant( 0 ) . PHP_EOL;echo round1stSignificant( -0.07468 ) . PHP_EOL;/** * Output * * 40000000 * 200000 * 0.08 * 0 * -0.08 * */

up

down

9

martinr at maarja dot net ¶16 years ago

Please note that the format of this functions output also depends on your locale settings. For example, if you have set your locale to some country that uses commas to separate decimal places, the output of this function also uses commas instead of dots.This might be a problem when you are feeding the rounded float number into a database, which requires you to separate decimal places with dots.See it in action:'. round('3.5558', 2);?>The output will be:3.563,56

up

down

4

dastra ¶11 years ago

round() will sometimes return E notation when rounding a float when the amount is small enough - see https://bugs.php.net/bug.php?id=44223 . Apparently it's a feature.To work around this "feature" when converting to a string, surround your round statement with an sprintf:sprintf("%.10f", round( $amountToBeRounded, 10));

up

down

9

php at silisoftware dot com ¶21 years ago

Here's a function to round to an arbitary number of significant digits. Don't confuse it with rounding to a negative precision - that counts back from the decimal point, this function counts forward from the Most Significant Digit. ex: Works on negative numbers too. $sigdigs should be >= 0 = 1) { $number /= 10; $multiplier *= 10; } return round($number, $sigdigs) * $multiplier; } ?>

up

down

2

goreyshi at gmail dot com ¶5 years ago

When you have a deal with money like dollars, you need to display it under this condition:-format all number with two digit decimal for cents.-divide 1000 by ,-round half down for number with more than two decimalI approach it using round function inside the number_format function:number_format((float)round( 625.371 ,2, PHP_ROUND_HALF_DOWN),2,'.',',') // 625.37number_format((float)round( 625.379 ,2, PHP_ROUND_HALF_DOWN),2,'.',',') // 625.38number_format((float)round( 1211.20 ,2, PHP_ROUND_HALF_DOWN),2,'.',',') // 1,211.20number_format((float)round( 625 ,2, PHP_ROUND_HALF_DOWN),2,'.',',') // 625.00

up

down

0

Hayley Watson ¶4 years ago

It should just be noted that what is called "precision" on this page is more correctly called accuracy; precision is the total number of significant digits on both sides of the decimal point, while accuracy is the number of digits to the right of the point. It's a common confusion.

up

down

1

feha at vision dot to ¶13 years ago

Here is a short neat function to round minutes (hour) ...You decide to round to nearest minute ...example will produce : 14:05

up

down

-2

omnibus at omnibus dot edu dot pl ¶13 years ago

Beware strange behaviour if number is negative and precision is bigger than the actual number of digits after comma.round(-0.07, 4);returns-0.07000000000000001So if you validate it against a regular expression requiring the maximum amount of digits after comma, you'll get into trouble.

up

down

-1

Astro ¶4 years ago

Okay, final version of my function:function NumberPrecision($n, $precision=0, $is_round=true) { if ($is_round) { $r = 5 * pow(10, -($precision+1)); $n += (($n < 0) ? -$r : $r); } $comma = '.'; $r = 5 * pow(10, -($precision+2)); $n += (($n > 0) ? -$r : $r); $n = number_format($n, $precision+1, $comma, ''); $n .= $comma; list($n, $frac) = explode($comma, $n, 2); $n = rtrim(rtrim($n, $comma) . $comma . substr($frac, 0, $precision), $comma); return ($n); }It can be useful in come cases when built-in function like round() or number_format() returns unexpected results. Works with positive and negative numbers, zero, numbers like 1/12, 0.3, numbers in scientific notation etc.

up

down

-1

terry at scribendi dot com ¶20 years ago

To round any number to a given number of significant digits, use log10 to find out its magnitude: Or when you have to display a per-unit price which may work out to be less than a few cents/pence/yen you can use: $dp)?$exp:$dp); ?> This always displays at least the number of decimal places required by the currency, but more if displaying the unit price with precision requires it - eg: 'English proofreading from $0.0068 per word', 'English beer from $6.80 per pint'.

up

down

-2

Anonymous ¶14 years ago

This functions return ceil($nb) if the double or float value is bigger than "$nb.5" else it's return floor($nb) $try) { return $sup; } return $inf; } ?>

up

down

-3

spectrumcat at gmail dot com ¶9 years ago

In case someone will need a "graceful" rounding (that changes it's precision to get a non 0 value) here's a simple function:function gracefulRound($val, $min = 2, $max = 4) { $result = round($val, $min); if ($result == 0 && $min < $max) { return gracefulRound($val, ++$min, $max); } else { return $result; }}Usage:$_ = array(0.5, 0.023, 0.008, 0.0007, 0.000079, 0.0000048);foreach ($_ as $val) { echo "{$val}: ".gracefulRound($val)."\n";}Output:0.5: 0.50.023: 0.020.008: 0.010.0007: 0.0010.000079: 0.00010.0000048: 0

up

down

-4

maxteiber at gmail dot com ¶17 years ago

the result of this function always depends on the underlying C function. There have been a lot of compiler bugs and floating-point precission problems involving this function. Right now the following code:returns:141.07on my machine.So never really trust this function when you do critical calculations like accounting stuff!Instead: use only integers or use string comparisons.

up

down

-4

Anonymous ¶6 years ago

This function has strange. behaviors:

up

down

-7

lossantis at ig dot com dot br ¶13 years ago

Since the mode parameter for options like PHP_ROUND_HALF_UP is available as of PHP 5.3, here is an alternative for ceiling: If I round this: You can also use a ceil (which might be useful for pagination): [Edited by: googleguy@php.net for clarity]

up

down

-5

php at persignum dot com ¶8 years ago

Because this function is missing round up and round down constants and the top note doesn't really show you how to round up or down to the nearest number, here is an easy way to always round up or always round down to the nearest number.int is the number you want to roundn is the nearest number you want rounded to.Round up to the nearest numberfunction round_up($int, $n) { return ceil($int / $n) * $n;}And to round down to the nearest numberfunction round_down(int, $n) { return floor($int / $n) * $n;}

up

down

-11

Bevan ¶14 years ago

Formats a number to the specified number of significant figures.

up

down

-10

armanhakimsagar at gmail dot com ¶6 years ago

$a = .9; $b = .8; $d = .1; $e = .2; $x = $a-$b; $y = $e-$d; $f = round($x,2); echo $f; if($f==$y){ echo "ok"; }

+add a note

Math 函数

abs

acos

acosh

asin

asinh

atan2

atan

atanh

base_​convert

bindec

ceil

cos

cosh

decbin

dechex

decoct

deg2rad

exp

expm1

fdiv

floor

fmod

hexdec

hypot

intdiv

is_​finite

is_​infinite

is_​nan

log10

log1p

log

max

min

octdec

pi

pow

rad2deg

round

sin

sinh

sqrt

tan

tanh

Copyright © 2001-2024 The PHP Group

My PHP.net

Contact

Other PHP.net sites

Privacy policy

PHP实现数据四舍五入的方法小结【4种方法】-腾讯云开发者社区-腾讯云

现数据四舍五入的方法小结【4种方法】-腾讯云开发者社区-腾讯云砸漏PHP实现数据四舍五入的方法小结【4种方法】关注作者腾讯云开发者社区文档建议反馈控制台首页学习活动专区工具TVP最新优惠活动文章/答案/技术大牛搜索搜索关闭发布登录/注册首页学习活动专区工具TVP最新优惠活动返回腾讯云官网砸漏首页学习活动专区工具TVP最新优惠活动返回腾讯云官网社区首页 >专栏 >PHP实现数据四舍五入的方法小结【4种方法】PHP实现数据四舍五入的方法小结【4种方法】砸漏关注发布于 2020-10-21 15:58:511.1K0发布于 2020-10-21 15:58:51举报文章被收录于专栏:恩蓝脚本恩蓝脚本本文实例总结了PHP实现数据四舍五入的方法。分享给大家供大家参考,具体如下:在PHP开发中,有时候我们会遇到将数据进行四舍五入的运算情况,本文分享了用PHP实现数据四舍五入的4种方法。php实现数据四舍五入的4种方法,分别通过number_format()函数、round()函数和sprintf()格式化以及intval()函数输出的方法实现四舍五入。严格来说最后一种不是严格的四舍五入,最后一种仅是将数据的小数部分去掉,只保留整数部分,详细情况会在下面进行讨论。1、number_format() 方法实现四舍五入number_format() 函数通过千位分组来格式化数字。

//定义一个float型的变量

$number = 1234.5678;

//English Notation (defult)

$number_format_english = number_format($number);

//1,235

$number_format_english = number_format($number, 2, '.', '');

//1234.57

//French Notation

$number_format_francais = number_format($number, 2, ',', '');

//1234,57

$number_format_francais = number_format($number, 3, ',', '');

//1234,568

echo $number_format_english;

//1234.57

echo $number_format_francais;

//1234,568复制2、round()方法实现四舍五入

round() 函数对浮点数进行四舍五入。

//定义一个float型的变量

$number = 1234.5678;

//不保留小数

echo round($number);

//1235

//保留两位小树

echo round($number,2);

//1234.57

echo "

$number = 12345678;

//在千分位进行四舍五入

echo round($number,-4);

//12350000复制3、sprintf() 格式化输入实现四舍五入字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。sprintf 是个变参函数。

//定义一个正整数

$n = 43951789;

//定义一个负整数

$u = -43951789;

// ASCII 65 is 'A'

$c = 65;

printf("%%b = '%b'\n", $n);

//%b = '10100111101010011010101101'

printf("%%c = '%c'\n", $c);

//%c = 'A'

printf("%%d = '%d'\n", $n);

//%d = '43951789'

printf("%%e = '%e'\n", $n);

//%e = '4.395179e+7'

printf("%%u = '%u'\n", $n);

//%u = '43951789'

printf("%%u = '%u'\n", $u);

//%u = '4251015507'

printf("%%f = '%f'\n", $n);

//%f = '43951789.000000'

printf("%%o = '%o'\n", $n);

//%o = '247523255'

printf("%%s = '%s'\n", $n);

//%s = '43951789'

printf("%%x = '%x'\n", $n);

//%x = '29ea6ad'

printf("%%X = '%X'\n", $n);

//%X = '29EA6AD'

printf("%%+d = '%+d'\n", $n);

//%+d = '+43951789'

printf("%%+d = '%+d'\n", $u);

//%+d = '-43951789'复制4、intval()函数实现整数输出此方法不是严格的四舍五入执行,其将数据的小数部分强制清除达到只输出整数部分的效果。

//定义一个浮点数

$number = 1234.5678;

$number_int = intval($number);

echo $number_int;

//1234复制PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:在线一元函数(方程)求解计算工具:

http://tools.zalou.cn/jisuanqi/equ_jisuanqi科学计算器在线使用_高级计算器在线计算:

http://tools.zalou.cn/jisuanqi/jsqkexue在线计算器_标准计算器:

http://tools.zalou.cn/jisuanqi/jsq更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数学运算技巧总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php正则表达式用法总结》希望本文所述对大家PHP程序设计有所帮助。本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。 原始发表:2020-09-11

,如有侵权请联系 cloudcommunity@tencent.com 删除前往查看phphttppython编程算法本文分享自 作者个人站点/博客 前往查看如有侵权,请联系 cloudcommunity@tencent.com 删除。本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!phphttppython编程算法评论登录后参与评论0 条评论热度最新登录 后参与评论推荐阅读LV.关注文章0获赞0领券社区专栏文章阅读清单互动问答技术沙龙技术视频团队主页腾讯云TI平台活动自媒体分享计划邀请作者入驻自荐上首页技术竞赛资源技术周刊社区标签开发者手册开发者实验室关于社区规范免责声明联系我们友情链接腾讯云开发者扫码关注腾讯云开发者领取腾讯云代金券热门产品域名注册云服务器区块链服务消息队列网络加速云数据库域名解析云存储视频直播热门推荐人脸识别腾讯会议企业云CDN加速视频通话图像分析MySQL 数据库SSL 证书语音识别更多推荐数据安全负载均衡短信文字识别云点播商标注册小程序开发网站监控数据迁移Copyright © 2013 - 2024 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有 深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569腾讯云计算(北京)有限责任公司 京ICP证150476号 |  京ICP备11018762号 | 京公网安备号11010802020287问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档Copyright © 2013 - 2024 Tencent Cloud.All Rights Reserved. 腾讯云 版权所有登录 后参与评论00

PHP: number_format - Manual

PHP: number_format - Manual

Downloads

Documentation

Get Involved

Help

Getting Started

Introduction

A simple tutorial

Language Reference

Basic syntax

Types

Variables

Constants

Expressions

Operators

Control Structures

Functions

Classes and Objects

Namespaces

Enumerations

Errors

Exceptions

Fibers

Generators

Attributes

References Explained

Predefined Variables

Predefined Exceptions

Predefined Interfaces and Classes

Predefined Attributes

Context options and parameters

Supported Protocols and Wrappers

Security

Introduction

General considerations

Installed as CGI binary

Installed as an Apache module

Session Security

Filesystem Security

Database Security

Error Reporting

User Submitted Data

Hiding PHP

Keeping Current

Features

HTTP authentication with PHP

Cookies

Sessions

Dealing with XForms

Handling file uploads

Using remote files

Connection handling

Persistent Database Connections

Command line usage

Garbage Collection

DTrace Dynamic Tracing

Function Reference

Affecting PHP's Behaviour

Audio Formats Manipulation

Authentication Services

Command Line Specific Extensions

Compression and Archive Extensions

Cryptography Extensions

Database Extensions

Date and Time Related Extensions

File System Related Extensions

Human Language and Character Encoding Support

Image Processing and Generation

Mail Related Extensions

Mathematical Extensions

Non-Text MIME Output

Process Control Extensions

Other Basic Extensions

Other Services

Search Engine Extensions

Server Specific Extensions

Session Extensions

Text Processing

Variable and Type Related Extensions

Web Services

Windows Only Extensions

XML Manipulation

GUI Extensions

Keyboard Shortcuts?

This help

j

Next menu item

k

Previous menu item

g p

Previous man page

g n

Next man page

G

Scroll to bottom

g g

Scroll to top

g h

Goto homepage

g s

Goto search(current page)

/

Focus search box

ord »

« nl2br

PHP 手册 函数参考 文本处理 字符串 字符串 函数

Change language:

English

German

Spanish

French

Italian

Japanese

Brazilian Portuguese

Russian

Turkish

Chinese (Simplified)

Other

Submit a Pull Request

Report a Bug

number_format

(PHP 4, PHP 5, PHP 7, PHP 8)number_format — 以千位分隔符方式格式化一个数字

说明

number_format(    float $num,    int $decimals = 0,    ?string $decimal_separator = ".",    ?string $thousands_separator = ","): string

使用四舍五入的规则,将数字格式化为千位分组和小数位(可选)。

参数

num

要格式化的数字。

decimals

设置小数位数。如果为 0,则从返回值中忽略 decimal_separator。

decimal_separator

指定小数点的分隔符。

thousands_separator

设置千位分隔符。

返回值

num 的格式化版本。

更新日志

版本

说明

8.0.0

在此版本之前,number_format() 接受一个、两个或四个参数(不会是三个)。

7.2.0

number_format() 现在再也不会返回 -0,之前

num 为 -0.01 的情况下可以返回 -0。

示例

示例 #1 number_format() 示例

例如,法语计数通常使用两位小数,逗号(“,”)作为小数分隔符,空格(“ ”)作为千位分隔符。以下示例展示了格式化数字的各种方法:

参见

money_format() - 将数字格式化成货币字符串

sprintf() - 返回格式化字符串

printf() - 输出格式化字符串

sscanf() - 根据指定格式解析输入的字符

+add a note

User Contributed Notes 10 notes

up

down

417

thomas at weblizards dot de ¶15 years ago

It's not explicitly documented; number_format also rounds:".number_format($number, 2, '.', ',')."
";?>0.001->0.000.002->0.000.003->0.000.004->0.000.005->0.010.006->0.010.007->0.010.008->0.010.009->0.01

up

down

10

Jeroen de Bruijn [NL] ¶18 years ago

If you want to display a number ending with ,- (like 200,-) when there are no decimal characters and display the decimals when there are decimal characters i use:function DisplayDouble($value) { list($whole, $decimals) = split ('[.,]', $value, 2); if (intval($decimals) > 0) return number_format($value,2,".",","); else return number_format($value,0,".",",") .",-"; }

up

down

10

info at ensostudio dot ru ¶2 years ago

Note: use NumberFormatter to convert in human-readable format instead user function from comments:format(12309); // twelve thousand three hundred nineecho NumberFormatter::create('ru', NumberFormatter::SPELLOUT)->format(12307.5); // двенадцать тысяч триста семь целых пять десятых?>

up

down

36

james at bandit dot co.nz ¶14 years ago

Outputs a human readable number.1000000000000) return round(($n/1000000000000),1).' trillion'; else if($n>1000000000) return round(($n/1000000000),1).' billion'; else if($n>1000000) return round(($n/1000000),1).' million'; else if($n>1000) return round(($n/1000),1).' thousand'; return number_format($n); }?>Outputs:247,704,360 -> 247.7 million866,965,260,000 -> 867 billion

up

down

21

MarcM ¶18 years ago

For Zero fill - just use the sprintf() function$pr_id = 1;$pr_id = sprintf("%03d", $pr_id);echo $pr_id;//outputs 001-----------------$pr_id = 10;$pr_id = sprintf("%03d", $pr_id);echo $pr_id;//outputs 010-----------------You can change %03d to %04d, etc.

up

down

7

Theo Diem ¶20 years ago

formatting numbers may be more easy if u use number_format function.I also wrote this :function something($number){ $locale = localeconv(); return number_format($number, $locale['frac_digits'], $locale['decimal_point'], $locale['thousands_sep']);}hope this helps =)[]'s

up

down

16

stm555 at hotmail dot com ¶18 years ago

I ran across an issue where I wanted to keep the entered precision of a real value, without arbitrarily rounding off what the user had submitted.I figured it out with a quick explode on the number before formatting. I could then format either side of the decimal.

up

down

-1

liviu andrei (bls) ¶12 years ago

To prevent the rounding that occurs when next digit after last significant decimal is 5 (mentioned by several people below):have fun!

up

down

-1

Mad Max ¶4 months ago

India (~18% of the world's population), Pakistan (~3%), Bangladesh (~2%), Nepal, and Myanmar, have a different way to display large numbers:10,23,45,678.20The first thousand is "normal" but then there is a comma every TWO digits after that.Now look at the number_format() function. Do you see a way to control the placement and frequency of the commas? Or a mode option/switch to deal with displaying numbers for almost 1/4 of the total population on Earth? Neither do I.If you are developing an application for an international audience, the ICU-based Intl PHP extension might work fine for you. However, IBM's ICU library is a resource heavy library (because...IBM), isn't the fastest library on the planet (also normal for IBM), ICU formatters are somewhat unreliable/inconsistent (yup, still IBM), and the Intl extension isn't always available in PHP (this one's PHP). So there are many significant hurdles to overcome just to prepare a number for display using the Intl extension.In short, approximately 23% of the planet currently can't use this function for displaying large numbers and there isn't a good built-in *lightweight* alternative other than to roll your own solution that outright replaces this function. Formatting a number for display is an extremely common task.Disclaimer: This comment serves as a warning to others that this function isn't suitable for an international audience in its current state. This comment is not a question, bug report, or feature request. I'm simply commenting on the current state of affairs with this function. If someone opts to make changes/improvements, then great, but that's not the purpose here.

up

down

-1

support at piri dot sk ¶10 months ago

My simpler solution to the problem of the decimal number in this function being longer than the specified number of decimals.Standard result for number_format() is..number_format(5.00098, 2) = 5.00My function will return the result = 5.0011) { $b = str_split($a[1]); $pos = 1; foreach ($b as $value) { if ($value != 0 && $pos >= $def) { $c = number_format($n, $pos); $c_len = strlen(substr(strrchr($c, "."), 1)); if ($c_len > $def) { return rtrim($c, 0); } return $c; // or break } $pos++; } } return number_format($n, $def);}?>

+add a note

字符串 函数

addcslashes

addslashes

bin2hex

chop

chr

chunk_​split

convert_​uudecode

convert_​uuencode

count_​chars

crc32

crypt

echo

explode

fprintf

get_​html_​translation_​table

hebrev

hex2bin

html_​entity_​decode

htmlentities

htmlspecialchars_​decode

htmlspecialchars

implode

join

lcfirst

levenshtein

localeconv

ltrim

md5_​file

md5

metaphone

money_​format

nl_​langinfo

nl2br

number_​format

ord

parse_​str

print

printf

quoted_​printable_​decode

quoted_​printable_​encode

quotemeta

rtrim

setlocale

sha1_​file

sha1

similar_​text

soundex

sprintf

sscanf

str_​contains

str_​decrement

str_​ends_​with

str_​getcsv

str_​increment

str_​ireplace

str_​pad

str_​repeat

str_​replace

str_​rot13

str_​shuffle

str_​split

str_​starts_​with

str_​word_​count

strcasecmp

strchr

strcmp

strcoll

strcspn

strip_​tags

stripcslashes

stripos

stripslashes

stristr

strlen

strnatcasecmp

strnatcmp

strncasecmp

strncmp

strpbrk

strpos

strrchr

strrev

strripos

strrpos

strspn

strstr

strtok

strtolower

strtoupper

strtr

substr_​compare

substr_​count

substr_​replace

substr

trim

ucfirst

ucwords

vfprintf

vprintf

vsprintf

wordwrap

Deprecated

convert_​cyr_​string

hebrevc

utf8_​decode

utf8_​encode

Copyright © 2001-2024 The PHP Group

My PHP.net

Contact

Other PHP.net sites

Privacy policy

round() 函数: 四舍五入取整法 | PHP 常用函数 |《PHP 知识点整理》| PHP 技术论坛

round() 函数: 四舍五入取整法 | PHP 常用函数 |《PHP 知识点整理》| PHP 技术论坛

PHP

话题列表

社区 Wiki

优质外文

招聘求职

PHP 实战教程

社区文档

登录

注册

PHP 知识点整理

展开或关闭

PHP 简介

PHP 是什么

PHP 作者及开发团队和版本

PHP 主要开发方向

正确的学习方法

PHP 开发环境和工具

什么是开发环境

PHP 基础知识

基本语法

PHP 注释

PHP 版本选择和维护周期

PHP 报错

输出:echo 语句

var_dump() 函数

include和require:包含文件

PHP 变量和常量

变量

常量

变量和常量的区别

PHP 预定义变量

$_POST 变量

$_GET 变量

PHP 数据类型

9 种数据类型

Boolean (布尔类型)

float (浮点型)

integer (整型)

srting (字符串)

array(数组)

—— 索引数组

—— 关联数组

—— 多维数组

object (对象)

NULL 类型

资源(resource)

伪类型

PHP 运算符

算术运算符

字符串运算符

赋值运算符

比较运算符

逻辑运算符

运算符优先级表

位运算符

数组运算符

执行运算符

类型运算符

递增/递减运算符

错误控制运算符

PHP 条件语句

单一条件(if)

双向条件(else)

多向条件(elseif)

多向条件(switch)

PHP 循环与控制

流程控制

while 语句

do…while 语句

break 中断语句

continue 跳过语句

exit 退出语句

return 语句

for 循环:指定次数的循环

foreach 循环:遍历数组

PHP 函数

构造函数

什么是函数

PHP 常用函数

empty() 函数:检查变量是否为空

trim( ) 函数:移除字符空格

ceil() 函数:向上取整

Session( ) 函数

header( ) 函数 : 跳转

exit( ) 和 die( ) 函数 :退出

file_exists() 函数:检查文件是否存在

floor() 函数:向下取整函数

round() 函数: 四舍五入取整法

password_hash() 和 password_verify() :密码加密与验证

数据库

数据库(Database)

关系型数据库管理系统(RDBMS)

PHP 数据对象 (PDO)

MySQL

PHP

首页

Laravel

Go

PHP

Python

Vue.js

Java

MySQL

Rust

LK

Elasticsearch

F2E 前端

程序员

Server

Database

DevTools

Computer Science

手机开发

AdonisJS

社区

Wiki

文档

社区文档首页

《PHP 内核与原生扩展开发》

《Composer 中文文档》

《Elasticsearch-PHP 中文文档》

《PHP PSR 标准规范》

《PHP 设计模式全集》

登录

注册

微信登录

提交改进

round() 函数: 四舍五入取整法

PHP 知识点整理

/

未匹配的标注

取整函数主要有三种:ceil()、floor()、round()ceil() :进一法取整,向上取整> floor() :舍去法取整,向下取整> round() : 四舍五入取整

round() 函数 简介

跟数学中的作用一样,四舍五入

round 在英文中是有大约,环绕,在某某四周,附近的意思,所以,可以取其大约的意思,在函数中是四舍五入,

注意:round() 返回的类型仍然是 round

演示 1 :

$a = 5;

$b = 3;

echo '不四舍五入:' . $a / $b ;

echo '
四舍五入:' . round($a / $b) ;

输出:

演示2:

$a = [

'-1.6' => -1.6,

'-1.3' => -1.3,

'-1' => -1,0,

'1' => 1,

'1.3' => 1.3,

'1.6' =>1.6

];

foreach($a as $key => $k ){

echo $key . ' 的 round 结果:' . round($k) . '
';

输出:

由上可知,四舍五入的时候,正数,小数位大于 5,则整数位加一,小数位小于 5,则整数位不变,抹除小数位;负数,小数位小于 5,则整数位不变,抹除小数位,小数位大于 5,则整数位加一;整数,则不变。

本文章首发在 LearnKu.com 网站上。

上一篇

下一篇

Markdown 文本

纠错改进

不正

版主

783 声望

最近还是一样努力着。

推荐文章:

更多推荐...

博客

PHP 核心特性 - 匿名函数

26

/

10

|

4年前

博客

Hyperf 常用助手函数

15

/

4

|

4年前

博客

Api接口实战:常用函数集合

58

/

8

|

4年前

翻译

PHP 7.4 新语法:箭头函数

12

/

7

|

4年前

翻译

100 个最常用的 PHP 函数

53

/

5

|

4年前

博客

每日五个 PHP 函数(2)

10

/

7

|

5年前

讨论数量: 0

发起讨论

只看当前版本

暂无话题~

社区赞助商

成为赞助商

关于 LearnKu

LearnKu 是终身编程者的修道场

做最专业、严肃的技术论坛

LearnKu 诞生的故事

资源推荐

《社区使用指南》

《文档撰写指南》

《LearnKu 社区规范》

《提问的智慧》

服务提供商

其他信息

成为版主

所有测验

联系站长(反馈建议)

粤ICP备18099781号-6

|

粤公网安备 44030502004330号

|

违法和不良信息举报

由 Summer 设计和编码 ❤

请登录

提交

忘记密码?

or

注册

第三方账号登录

微信登录

GitHub 登录

内容举报

匿名举报,为防止滥用,仅管理员可见举报者。

我要举报该,理由是:

垃圾广告:恶意灌水、广告、推广等内容

无意义内容:测试、灌水、文不对题、消极内容、文章品质太差等

违规内容:色情、暴利、血腥、敏感信息等

不友善内容:人身攻击、挑衅辱骂、恶意行为

科学上网:翻墙、VPN、Shadowsocks,政策风险,会被关站!

不懂提问:提问太随意,需要再做一遍《提问的智慧》测验

随意提问:提问没有发布在社区问答分类下

排版混乱:没有合理使用 Markdown 编写文章,未使用代码高亮

内容结构混乱:逻辑不清晰,内容混乱,难以阅读

标题随意:标题党、标题不释义

尊重版权:分享付费课程、破解软件(付费),侵犯作者劳动成果

其他理由:请补充说明

举报

取消

PHP四舍五入函数代码详解-php教程-PHP中文网

PHP四舍五入函数代码详解-php教程-PHP中文网

登录  /  注册

首页

PHP培训新

视频教程

视频课程

直播课程

精品课

学习路径

入门教程

独孤九贱

玉女心经

天龙八部

趣味闯关

资源下载

源码市场

工具下载

在线工具

手册下载

电子课件

js特效

网站源码

网站素材

类库下载

技术文章

前端开发

后端开发

数据库

php框架

每日编程

社区

问答

博客

文章

专题

微信公众号

扫码关注官方订阅号

编程词典

APP下载

源码市场

php开发

前端

HTML|

CSS|

JavaScript|

Vue.js

后端

PHP|

ThinkPHP|

Laravel|

MySQL|

Redis

最新推荐

php8,我来也

84669人学习

细说PHP(2021版)第一季

65727人学习

TP6.0 搭建个人博客实战(玉女心经版)

82984人学习

2018前端入门_HTML5

467778人学习

大前端

原生基础

HTML|

CSS|

HTML5|

CSS3|

JavaScript

框架开发

jQuery|

Vue.js|

React|

AngularJS|

Node.js|

BootStrap|

AJAX|

Foundation

最新推荐

JavaScript极速入门_玉女心经系列

498837人学习

独孤九贱(1)_HTML5视频教程

471966人学习

CSS视频教程-玉女心经版

256484人学习

30分钟学会网站布局

152542人学习

后端开发

编程语言

PHP|

Python|

Go|

Java|

C|

C++|

C#|

VBSscript|

Scala|

Lua|

Perl|

Ruby|

JSP|

XML|

ASP

框架/工具

ThinkPHP|

Laravel|

Servlet|

Django|

ASP.NET

最新推荐

Thinkphp6.0正式版视频教程

224170人学习

php8,我来也

84669人学习

PHP实战天龙八部之微信支付视频教程

139536人学习

CI框架30分钟极速入门

81804人学习

数据库

基础入门

MySQL|

SQL Server

进阶学习

MongoDB|

Oracle|

Redis|

Memcached

最新推荐

MySQL权威开发指南(教程)

85022人学习

Redis基础视频课程

11944人学习

尚观Oracle入门到精通视频教程

20001人学习

PDO操作极速入门,今天你用了吗?

60816人学习

移动端

原生开发

Android|

iOS

多端开发

Swift|

Flutter|

uni-app|

小程序|

其他

最新推荐

你的第一行UNI-APP代码

5487人学习

Uniapp简爱读书项目开发--第一季

15007人学习

公益直播:Uniapp微信小程序1:1仿饿了么首页

2150人学习

Flutter从零到APP上架

6980人学习

运维开发

环境使用

Linux|

Docker

工具使用

PhpStudy|

Git|

其他工具

最新推荐

phpStudy V8 视频教程

194925人学习

兄弟连新版Linux视频教程

359900人学习

Git教程(60分钟全程无废话版)

1142人学习

vscode其实很简单

19058人学习

UI设计

UI设计

Axure|

PS

最新推荐

AXURE 9视频教程(适合产品经理 交互 产品设计 UI)

3206人学习

零基础精通 PS 视频教程

180550人学习

16天带你入门UI视频教程

48569人学习

PS技法与切片技术视频教程

17603人学习

计算机基础

类库分类

HTTP|

TCP/IP|

编程基础

最新推荐

阿里云环境搭建以及项目上线视频教程

40936人学习

计算机网络概述—程序员必须掌握的基础知识

1049人学习

程序员入门必备教程—HTTP协议详解

750人学习

Websocket视频教程

32909人学习

首页 >

后端开发 >

php教程 >

正文

PHP四舍五入函数代码详解

藏色散人

发布: 2019-04-11 10:01:44

原创

3420人浏览过

本篇文章主要给大家介绍PHP四舍五入的相关函数使用方法,即intval()函数、round()函数、ceil()函数、floor()函数,希望对需要的朋友有所帮助!

    

一、intval()函数

代码示例:

$num = 3.1415926;

$num2 = 3.6;

echo intval($num).'
';

echo intval($num2);

echo "


";登录后复制输出:3

3登录后复制登录后复制二、round()函数代码示例:

$num = 3.1415926;

$num2 = 3.6;

echo round($num).'
';

echo round($num2);

echo "


";登录后复制输出:3

4登录后复制三、ceil()函数

$num = 3.1415926;

$num2 = 3.6;

echo ceil($num).'
';

echo ceil($num2);

echo "


";登录后复制输出:4

4登录后复制四、floor()函数

$num = 3.1415926;

$num2 = 3.6;

echo floor($num).'
';

echo floor($num2);登录后复制输出:3

3登录后复制登录后复制PHP四舍五入函数总结:

intval()函数表示获取变量的整数值。

round()函数表示对浮点数进行四舍五入。

ceil()函数表示进一法取整。

floor()函数表示舍去法取整。

相关推荐:《PHP教程》

以上就是PHP四舍五入函数代码详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答

PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。

我要提问

来源:php中文网

收藏

点赞

上一篇:socket套接字详解(TCP与UDP)

下一篇:PHP如何将十进制转换为十六进制?(代码示例)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

作者最新文章

2023年最流行的10款前端UI框架排名

2024-02-06 10:58:58

enterprise是什么版本

2023-10-08 17:23:38

502 bad gateway nginx什么意思

2023-10-08 17:06:30

Word文档拆分后的子文档字体格式变了怎么办

2023-06-16 17:23:30

linux中dev是什么意思

2023-05-20 13:55:09

后盾网php培训怎么样

2023-05-18 16:12:41

六星教育是培训机构吗

2023-05-18 16:12:01

北风培训机构多少年啦

2023-05-18 16:09:02

黑马培训机构有啥专业

2023-05-18 16:03:02

传智播客php培训多少钱

2023-05-18 15:57:02

最新问题

MySQL:从聚合结果中检索分组行的总和

假设我有每个国家用户访问次数的统计:查询如下:SELECTcountries_users.user_id,countries.name,count(countries_users....

P粉978742405来自于2024-02-26 23:21:28

0

1

381

寻找模式的多个出现,使用正则表达式

我得到了这个字符串:if(条件A==值A-AND-条件B==值B-OR-条件C==值C)我想要一个包含以下内容的数组:array(3){[0]=>string(...)&qu...

P粉186017651来自于2024-02-26 23:27:41

0

1

1277

如何允许chrome扩展访问他的路径和文件

我正在制作Opera扩展,我想获取存储在扩展路径中的一些图像。该插件的作用是,在特定网页上,将背景图像URL和一些图标更改为扩展程序内的自定义图标。但是,当我运行脚本时,所有内容都...

P粉644981029来自于2024-02-26 23:12:34

0

1

360

使用“可见值”选项进行选择后导航到下一页

我正在尝试使用HTML创建一种流程(代码在底部)我选择“Cisco”,然后进入下一个框供我选择类型,我选择“PE路由器”,然后进入下一组选项,选择设备,依此类推,直到完成我如何创建...

P粉351138462来自于2024-02-26 22:45:32

0

1

485

解释为表单中的动画图像元素提交的图像坐标

我想通过检查php$_POST数组来捕获指示用户单击动画图像的位置的X:Y值。问题是,虽然我可以获取静态图像上的输入提交信息,但我似乎无法从动画图像中获取任何值。以下是演示此行为的...

P粉421119778来自于2024-02-26 22:34:14

0

1

337

无法在我的创建模块中显示类别图像

我想在我自己的模块中显示caregory图像,所以我创建了一个模块和frontend/templates/category-section.phtml,这是代码,我的问题是源图像是...

P粉731977554来自于2024-02-26 22:31:22

0

1

277

如何从另一个js文件激活我的nodejs功能?

我有一个数据库,其中存储了登录我网站的所有用户,并希望他们能够删除他们的帐户。所以我做了一个可以工作的函数,可以删除用户的accs,但我遇到的唯一问题是我似乎无法将具有按钮事件侦听...

P粉833546953来自于2024-02-26 22:35:53

0

1

252

ErrorException 尝试获取属性“nom_service”非对象的

请帮助我尝试了最大的方法但没有结果,我是Laravel的初学者!!!liste.blade.php{{$data->candidature->dem...

P粉662089521来自于2024-02-26 22:38:44

0

1

337

在 dash 应用程序中,如何交换不同容器的内容?

我在做以下事情时遇到了很大的困难。我有一个由css格式化的破折号应用程序,以显示在不同的容器中。标记为“hexgrid-1-container”的容器是最大的容器,而其他容器较小,...

P粉378890106来自于2024-02-26 22:35:55

0

1

295

如何在MySQLWorkBench中指定日期函数以避免错误提示?

我尝试创建一个表,这就是我的设置方式:CREATETABLEemp_tab(empnoNUMeric(10),nameVARCHAR(50)NOTNULL,jobVARCHAR(5...

P粉245003607来自于2024-02-26 21:56:48

0

1

289

相关专题

更多>

oa系统哪个比较好

字符串常量的表示方法

网络安全技术有哪些

excel怎么排序

cad中毒了如何解决

jquery each

如何解决Java堆栈溢出异常

microsoft project

热门推荐

PHP除法运算中四舍五入取整、向上取整、向下取整、小数截取的使用

PHP四舍五入、取整、round函数使用

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

php float不四舍五入截取浮点型字符串的方法

热门教程

更多>

相关推荐

热门推荐

最新课程

ThinkPHP6.0极速入门(视频教程)

358948次学习

收藏

ThinkPHP5基础视频教程

35743次学习

收藏

PHP开发编码规范

242589次学习

收藏

最新ThinkPHP 5.1全球首发视频教程(60天成就PHP大牛线上培训班课)

1368814次学习

收藏

独孤九贱(4)_PHP视频教程

1185872次学习

收藏

PHP实战天龙八部之仿爱奇艺电影网站

735232次学习

收藏

PHP入门视频教程之一周学会PHP

1435730次学习

收藏

ThinkPHP5实战之[教学管理系统]

695052次学习

收藏

微信小程序开发之API篇

667次学习

收藏

Webpack4.x---十天技能课堂

1352次学习

收藏

Bootstrap4.x---十天精品课堂

1637次学习

收藏

ECMAScript6 / ES6---十天技能课堂

1831次学习

收藏

Laravel---API接口

670次学习

收藏

最新下载

更多>

网站特效

网站源码

网站素材

前端模板

[表单按钮] jQuery企业留言表单联系代码

[播放器特效] HTML5 MP3音乐盒播放特效

[菜单导航] HTML5炫酷粒子动画导航菜单特效

[表单按钮] jQuery可视化表单拖拽编辑代码

[播放器特效] VUE.JS仿酷狗音乐播放器代码

[html5特效] 经典html5推箱子小游戏

[图片特效] jQuery滚动添加或减少图片特效

[相册特效] CSS3个人相册封面悬停放大特效

[Bootstrap模板] 有机果蔬供应商网页模板 Bootstrap5

[后端模板] Bootstrap3多功能数据信息后台管理响应式网页模板-Novus

[Bootstrap模板] 房产资源服务平台网页模板 Bootstrap5

[Bootstrap模板] 简约简历资料网页模板 Bootstrap4

[Bootstrap模板] bootstrap响应式宽屏图书教育网站模板-DREAMLIFE

[后端模板] MAC风格响应式蓝色企业CMS后台管理系统模版

[后端模板] 响应式渐变大气后台管理系统网站模板-usinessbox

[Bootstrap模板] 响应式蔬菜水果商店网站模板-Organio

[网站素材] 可爱的夏天元素矢量素材(EPS+PNG)

[网站素材] 四个红的的 2023 毕业徽章矢量素材(AI+EPS+PNG)

[网站素材] 唱歌的小鸟和装满花朵的推车设计春天banner矢量素材(AI+EPS)

[网站素材] 金色的毕业帽矢量素材(EPS+PNG)

[网站素材] 黑白风格的山脉图标矢量素材(EPS+PNG)

[网站素材] 不同颜色披风和不同姿势的超级英雄剪影矢量素材(EPS+PNG)

[网站素材] 扁平风格的植树节banner矢量素材(AI+EPS)

[网站素材] 九个漫画风格的爆炸聊天气泡矢量素材(EPS+PNG)

[前端模板] 家居装潢清洁维修服务公司网站模板

[前端模板] 清新配色个人求职简历引导页模板

[前端模板] 设计师创意求职简历网页模板

[前端模板] 现代工程建筑公司网站模板

[前端模板] 教育服务机构响应式HTML5模板

[前端模板] 网上电子书店商城网站模板

[前端模板] IT技术解决互联网公司网站模板

[前端模板] 紫色风格外汇交易服务网站模板

关于我们

免责申明

意见反馈

讲师合作

广告合作

最新更新

php中文网:公益在线php培训,帮助PHP学习者快速成长!

关注服务号

微信扫码关注PHP中文网服务号

技术交流群

QQ扫码加入技术交流群

app下载

扫描下载App

PHP中文网订阅号

每天精选资源文章推送

PHP中文网APP

随时随地碎片化学习

PHP中文网抖音号

发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

精品班

技术支持

技术咨询

学习群

会员优惠

返回顶部