Javascript:四舍五入到 5 的下一个倍数 - Javascript: Round up to the next multiple of 5

问题:

I need a utility function that takes in an integer value (ranging from 2 to 5 digits in length) that rounds up to the next multiple of 5 instead of the nearest multiple of 5. Here is what I got:我需要一个实用函数,它接受一个整数值(长度为 2 到 5 位数字),该值向上取整为 5 的下一个倍数而不是最接近的 5 的倍数。这是我得到的:

function round5(x)
{
    return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
}

When I run round5(32) , it gives me 30 , where I want 35.当我运行round5(32) ,它给了我30 ,我想要 35 。
When I run round5(37) , it gives me 35 , where I want 40.当我运行round5(37) ,它给了我35 ,我想要 40 。

When I run round5(132) , it gives me 130 , where I want 135.当我运行round5(132) ,它给了我130 ,我想要 135 。
When I run round5(137) , it gives me 135 , where I want 140.当我运行round5(137) ,它给了我135 ,我想要 140 。

etc...等等...

How do I do this?我该怎么做呢?


解决方案:

参考: https://stackoom.com/en/question/1HWdk
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章