PHP输入百分比数字四舍五入到小数点后两位 [英] PHP enter percentage number round to 2 decimal places not working
本文介绍了PHP输入百分比数字四舍五入到小数点后两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
php的数字四舍五入到小数点后两位.我的数字是0.00000000000000000000001
,我希望结果为0.01
.我尝试了php number_format()函数php
php number round to 2 decimal places not working my number is 0.00000000000000000000001
and I want result as 0.01
. I tried php number_format() function php round() function but they didn't work for me.
推荐答案
只需创建一个简单的函数即可返回舍入值或所需的最小值...
Just make a simple function that returns either the rounded value or the minimum value you would like...
function round_special($x)
{
if ($x == 0) return 0;
$rounded = round($x, 2);
$minValue = 0.01;
if ($rounded < $minValue) {
return number_format($minValue, 2);
} else {
return number_format($rounded, 2);
}
}
因此,结果将如下所示:
So, results would look like:
$x = 0.00000000000000000000001;
echo round_special($x); // 0.01
echo round_special(0.0001); // 0.01
echo round_special(55); // 55.00
echo round_special(0.6); // 0.06
这篇关于PHP输入百分比数字四舍五入到小数点后两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文