1 Star 0 Fork 0

Nyx/Nyx Ulric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
float_2.php 952 Bytes
一键复制 编辑 原始数据 按行查看 历史
Nyx 提交于 2020-04-20 14:04 . php保留2位小数
<?php
$num = 10.4567;
// php保留两位小数并且四舍五入
// 第一种:利用round()对浮点数进行四舍五入
$result = round($num, 2);
echo $result; //10.46
echo "<hr />";
// 第二种:利用sprintf格式化字符串
$result = sprintf("%.2f", $num);
echo $result; //10.46
echo "<hr />";
// 第三种:利用千位分组来格式化数字的函数number_format()
$result = number_format($num, 2);
echo $result; //10.46
echo "<hr />";
//或者如下
$result = number_format($num, 2, '.', '');
echo $result; //10.46
echo "<hr />";
// php保留两位小数并且不四舍五入
$result = sprintf("%.2f", substr(sprintf("%.3f", $num), 0, -1));
echo $result;
echo "<hr />";
// php进一法取整
$result = ceil($num);//11
echo $result;
echo "<hr />";
// php舍去法取整
$result = floor($num);//10
echo $result;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/knicks/Nyx-Ulric.git
[email protected]:knicks/Nyx-Ulric.git
knicks
Nyx-Ulric
Nyx Ulric
master

搜索帮助