我需要一个效用函数,它接受一个整数值(长度从 2 到 5 位数字),该整数值四舍五入到 下一个 5 的倍数而不是 最接近 的 5 的倍数。这是我得到的:
function round5(x)
{
return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
}
当我运行 round5(32)
时,它给了我 30
,我想要 35。
当我运行 round5(37)
时,它给了我 35
,我想要 40。
当我运行 round5(132)
时,它给了我 130
,我想要 135。
当我运行 round5(137)
时,它给了我 135
,我想要 140。
ETC…
我该怎么做呢?
原文由 Amit Erandole 发布,翻译遵循 CC BY-SA 4.0 许可协议
这将完成工作:
It’s just a variation of the common rounding
number
to nearest multiple ofx
functionMath.round(number/x)*x
, but using.ceil
instead of.round
根据数学规则使其始终向上舍入而不是向下/向上。