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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章