Javascript 四舍五入到最接近的0.5,而不是0.0

Javascript 四舍五入到最接近的0.5,而不是0.0,javascript,math,Javascript,Math,如何找到最接近的nb.5值 不包括.0值 例如 round(1.0) = 1.5 round(1.99) = 1.5 round(2.0) = 2.5 您可以将0.5添加到以下返回的值: const round=number=>Math.floornumber+0.5 console.loground1.0 console.loground1.99 console.loground2.0Math.floorvalue+0.5应该可以 此外,你应该澄清你的规格。。。因为最接近的0.5值2是1.

如何找到最接近的nb.5值

不包括.0值

例如

round(1.0) = 1.5
round(1.99) = 1.5
round(2.0) = 2.5
您可以将0.5添加到以下返回的值:

const round=number=>Math.floornumber+0.5 console.loground1.0 console.loground1.99 console.loground2.0Math.floorvalue+0.5应该可以

此外,你应该澄清你的规格。。。因为最接近的0.5值2是1.5和2.5,所以它们都位于相同的距离

我知道您的示例通过最接近的上限0.5值来处理这种情况,但这真的是您想要的吗?

函数roundnum{ 返回Math.roundnum%10+0.5 } console.loground1; console.loground1.99;
console.loground2 要获得预期结果,请使用Math.trunc使用以下选项-

让round=val=>Math.truncval+0.5 console.loground1.99 console.loground1
console.loground2只是好奇,第一轮呢?应该遵循同样的逻辑,-0.01=-0.5,-0.99=-0.5,-1.00=-1.5,-等等:太棒了,谢谢,我完全没想到会这样做。。我不确定哪一个答案是第一个,但你的答案似乎是最详细的,当我被允许的时候,你会接受。@user2267175我回答了10秒之前,因为IEatBagels正在忙着吃东西bagels@adiga看来这将是我的垮台