简体   繁体   English

PHP舍入到小数点后第二位

[英]PHP to round up to the 2nd decimal place

By calculating areas I have a number which I need to display in a strange way. 通过计算面积,我得到了一个需要以奇怪的方式显示的数字。

Always display 2 decimal places. 始终显示两位小数。 Always round up the 2nd decimal place if the 3rd+ decimal places > 0. 如果第三位+小数位数> 0,则始终将第二位小数舍入。

Examples: 例子:

0.5 = 0.50
0.500003 = 0.51
0.96531 = 0.97
0.96231 = 0.97
0.8701 = 0.88

Is there a built in function to do this in PHP or do I need to write one? 是否有内置函数可以在PHP中执行此操作,或者我需要编写一个函数?

You can use 2 functions: 您可以使用2个功能:

I've used both with success, and depending on what you're doing with the result, you may chose either of the above functions. 我既成功使用过,也根据结果做些什么,可以选择上述两种功能之一。

Later edit: If you want to only round up, you can use ceil() - http://www.php.net/manual/en/function.ceil.php + number format or round 后来编辑:如果你想只围捕,你可以使用小区() - http://www.php.net/manual/en/function.ceil.php +数字格式或圆

echo round(ceil($number*100)/100,2);

As another user suggested earlier 如另一位用户先前建议

To always round up you will want to use something like this: 为了始终取整,您将需要使用以下内容:

$number = 0.8701;

echo ceil($number*100)/100;

// = 0.88

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM