【发布时间】:2013-09-23 06:57:41
【问题描述】:
我需要一个实用函数,它接收一个整数值(长度范围为 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。
等等……
我该怎么做?
【问题讨论】:
-
round5(5)应该给 5 还是 10? -
怎么样:将 x 除以 5,四舍五入到最接近的整数(使用 Math.ceil 函数),然后乘以 5?
-
round5(5) 应该给 5
标签: javascript math rounding