[removed] Round up to the next multiple of 5

后端 未结 8 957
[愿得一人]
[愿得一人] 2020-12-02 09:39

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

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 09:55

    const roundToNearest5 = x => Math.round(x/5)*5
    

    This will round the number to the nearest 5. To always round up to the nearest 5, use Math.ceil. Likewise, to always round down, use Math.floor instead of Math.round. You can then call this function like you would any other. For example,

    roundToNearest5(21)
    

    will return:

    20
    

提交回复
热议问题