PHP 中四舍五入到最接近的五的倍数
我想要一个 php 函数,当用 52 调用它时返回 55。
我尝试了 round()
函数:
echo round(94, -1); // 90
它返回 90 但我想要 95。
谢谢。
I want a php function which returns 55 when calling it with 52.
I've tried the round()
function:
echo round(94, -1); // 90
It returns 90 but I want 95.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这可以通过多种方式完成,具体取决于您首选的舍入约定:
1. 舍入到下一个 5 的倍数,排除当前数字
行为:50 输出 55、52 输出 55
2. 舍入到最接近 5 的倍数,包括当前数字
行为:50 输出 50、52 输出 55、50.25 输出 50
3. 向上舍入到一个整数,然后是最接近的 5 倍数
行为:50 输出 50、52 输出 55、50.25 输出 55
This can be accomplished in a number of ways, depending on your preferred rounding convention:
1. Round to the next multiple of 5, exclude the current number
Behaviour: 50 outputs 55, 52 outputs 55
2. Round to the nearest multiple of 5, include the current number
Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 50
3. Round up to an integer, then to the nearest multiple of 5
Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 55
round()
(或ceil()
如果您想始终向上舍入)值 5(分辨率/粒度)可以是任何值 - 在步骤 1 和步骤 3 中替换它
所以总而言之:
round()
(orceil()
if you want to round up always)The value 5 (the resolution / granularity) can be anything — replaced it in both step 1 and 3
So in summary:
向下舍入:
向上舍入:
四舍五入到最接近的位置(向上或向下):
Round down:
Round up:
Round to closest (up or down):
我知道这是一个老问题,但恕我直言,使用模运算符是最好的方法,并且比公认的答案优雅得多。
I know it's an old question, but IMHO using modulus operator is the best way, and far more elegant than the accepted answer.
试试我写的这个小函数。
Try this little function I wrote.
来自 Gears 库
来源:
From Gears library
Source:
乘以 2,舍入为 -1,除以 2。
Multiply by 2, round to -1, divide by 2.
这是我的 Musthafa 函数版本。这个比较复杂,但它支持浮点数以及整数。要舍入的数字也可以位于字符串中。
我尝试了 Knight、Musthafa 的功能,甚至是 Musthafa 的建议="/users/58013/praesagus">Praesagus。他们不支持浮点数字以及 Musthafa's & 的解决方案。 Praesagus 在某些数字中无法正常工作。尝试以下测试数字并自己进行比较:
Here is my version of Musthafa's function. This one is more complex but it has support for Float numbers as well as Integers. The number to be rounded can also be in a string.
I tried the functions from Knight, Musthafa and even the suggestion from Praesagus. They don't have support for Float numbers and the solutions from Musthafa's & Praesagus do not work correctly in some numbers. Try the following test numbers and do the comparison yourself:
我这样做:
测试:
I do it like this:
Tests:
我刚刚在 20 分钟内写了这个函数,基于我到处发现的许多结果,我不知道它为什么起作用或者它是如何工作的! :D
我主要感兴趣的是将货币数字从 151431.1 LBP 转换为 150000.0 LBP。 (151431.1 LBP == ~100 USD) 到目前为止,它工作得很好,但是我试图让它以某种方式与其他货币和数字兼容,但不确定它是否工作正常!
如果有什么问题欢迎修改和修复
I just wrote this function in 20 min, based on many results I found here and there, I don't know why it works or how it works!! :D
I was mainly interested in converting currency numbers from this 151431.1 LBP to 150000.0 LBP. (151431.1 LBP == ~100 USD) which works perfectly so far, however I tried to make it somehow compatible with other currencies and numbers, but not sure if it works fine!!
Feel free to modify it and fix it if there's anything wrong
也许您也可以考虑这款衬里。更快了!适用于
$num >= 0
和$factor > 0 。
Probably you can also consider this one liner. It's faster! Works for
$num >= 0
and$factor > 0
.