搜索
首页后端开发PHP问题php中实现四舍五入的方法有哪些

php中实现四舍五入的方法有哪些

Nov 18, 2021 am 10:47 AM
php四舍五入

方法:1、使用number_format(),语法“number_format($num,小数位数,'.','')”;2、使用round(),语法“round($num,小数位数)”;3、利用sprintf()函数格式化输出来实现四舍五入。

php中实现四舍五入的方法有哪些

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

在PHP开发中,有时候我们会遇到将数据进行四舍五入的运算情况,下面分享用PHP实现数据四舍五入的3种方法,分别通过number_format()函数、round()函数和sprintf()格式化输出的方法实现四舍五入。

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

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

<?php
 
//定义一个float型的变量
$number = 1234.5678;
 
//English Notation (defult)
$number_format_english = number_format($number);
//1,235
$number_format_english = number_format($number, 2, &#39;.&#39;, &#39;&#39;);
//1234.57
 
//French Notation
$number_format_francais = number_format($number, 2, &#39;,&#39;, &#39;&#39;);
//1234,57
$number_format_francais = number_format($number, 3, &#39;,&#39;, &#39;&#39;);
//1234,568
 
echo $number_format_english;
//1234.57
echo $number_format_francais;
//1234,568

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

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

<?php
 
//定义一个float型的变量
$number = 1234.5678;
 
//不保留小数
echo round($number);
//1235
 
//保留两位小数
echo round($number,2);
//1234.57
 
echo "<br>";
 
$number = 12345678;
 
//在千分位进行四舍五入
echo round($number,-4);
//12350000

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

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

<?php
 
//定义一个正整数
$n =  43951789;
 
//定义一个负整数
$u = -43951789;
 
// ASCII 65 is &#39;A&#39;
$c = 65; 
 
printf("%%b = &#39;%b&#39;\n", $n); 
//%b = &#39;10100111101010011010101101&#39;
printf("%%c = &#39;%c&#39;\n", $c); 
//%c = &#39;A&#39; 
printf("%%d = &#39;%d&#39;\n", $n); 
//%d = &#39;43951789&#39; 
printf("%%e = &#39;%e&#39;\n", $n); 
//%e = &#39;4.395179e+7&#39; 
printf("%%u = &#39;%u&#39;\n", $n); 
//%u = &#39;43951789&#39; 
printf("%%u = &#39;%u&#39;\n", $u); 
//%u = &#39;4251015507&#39; 
printf("%%f = &#39;%f&#39;\n", $n); 
//%f = &#39;43951789.000000&#39; 
printf("%%o = &#39;%o&#39;\n", $n); 
//%o = &#39;247523255&#39; 
printf("%%s = &#39;%s&#39;\n", $n); 
//%s = &#39;43951789&#39; 
printf("%%x = &#39;%x&#39;\n", $n); 
//%x = &#39;29ea6ad&#39; 
printf("%%X = &#39;%X&#39;\n", $n); 
//%X = &#39;29EA6AD&#39; 
printf("%%+d = &#39;%+d&#39;\n", $n); 
//%+d = &#39;+43951789&#39; 
printf("%%+d = &#39;%+d&#39;\n", $u); 
//%+d = &#39;-43951789&#39;

推荐学习:《PHP视频教程

以上是php中实现四舍五入的方法有哪些的详细内容。更多信息请关注PHP中文网其他相关文章!

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具