问题描述
|
7/2 = 3.5
如何获得剩余的高数量?在此示例中,应为4,而不是3。
解决方法
您正在寻找Math.ceil函数:
Math.ceil(7/2); #4
天花板是天花板的缩写,天花板总是四舍五入,因此任何大于3的都将变成4。
相反的是Math.floor,它将始终四舍五入,因此任何<4都将变为3。
, 您想要Math.ceil()
代表正数,或Math.floor()
代表负数。
, 7/2中的余数是1。我认为您不是要问余数。
您的问题确实是\'我如何将十进制数字四舍五入到最接近的整数?\'-在这种情况下,3.5应该四舍五入为4,而3.4应该四舍五入为3?如果是这样,则需要Math.round()
函数:
Math.round(7/2) //returns 4 (3.5 rounded up).
Math.round(3.5) //returns 4 (3.5 rounded up).
Math.round(3.4) //returns 3 (3.4 rounded down).
Math.round(10/3) //returns 3 (3.33333333 rounded down).