php四舍五入数字
实际上,“四舍五入”可能不是描述我想要做的事情的最佳名称。
我只想对小数点后有零的浮点数进行舍入:“.00”
如果小数点后有零,我不想显示任何小数。 通常我只会在变量中查找“.00”,如果它有我要查找的内容,我就会将其删除。但是,在某些情况下,小数点可能会长于 2 位。比如: 45.000 或 56.0000
如果有一个 php 函数删除了多余的零(如果存在的话),我不想弄乱任何正则表达式。
Actually, "rounding" may not be the best name to describe what I'm looking to do.
I want to round only floats that have zeros after the decimal point: ".00"
I don't want to display any decimal numbers if there are zeros after the decimal point.
Normally I'd just look for ".00" in the variable, and if it has what I'm looking for I just strip it out. However, there are a few instances that the decimal might be longer than 2 places. like: 45.000 or 56.0000
I didn't want to mess with any regex on this if there was a php function that stripped out the extra zeros, if they exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
floatval()
可能可以解决您的问题。Using
floatval()
might be the solution to your problem.使用
round($f,2);
http://php.net/manual/en/function.round.php
对于那个投反对票的天才:http://codepad.org/2bBA5UpL
Use
round($f,2);
http://php.net/manual/en/function.round.php
For that genius who downvoted: http://codepad.org/2bBA5UpL