在 PHP 中将整数舍入到最接近的 5 的倍数 [英] Round integer to nearest multiple of 5 in PHP

查看:39
本文介绍了在 PHP 中将整数舍入到最接近的 5 的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索函数 ro 将数字四舍五入到最接近的 5 的倍数

Searching for a function ro round numbers to the nearest multiple of 5

22 -> 20
23 -> 25
40 -> 40
46 -> 45
48 -> 50

等等.

试过这个总是返回更高的值:

Tried this which always returns the higher value:

5 * ceil($n / 5);

推荐答案

使用 round() 而不是 ceil().

5 * round($n / 5);

ceil() 按顺序将浮点数向上舍入到下一个整数.round() 将使用标准舍入规则舍入到最接近的整数.

ceil() rounds a floating point number up to its next integer in sequence. round() will round to the nearest integer using standard rounding rules.

这篇关于在 PHP 中将整数舍入到最接近的 5 的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆