How to round an integer up or down to the nearest 10 using Javascript

后端 未结 4 1881
你的背包
你的背包 2020-11-27 19:04

Using Javascript, I would like to round a number passed by a user to the nearest 10. For example, if 7 is passed I should return 10, if 33 is passed I should return 30.

4条回答
  •  被撕碎了的回忆
    2020-11-27 19:13

    Divide the number by 10, round the result and multiply it with 10 again:

    var number = 33;
    console.log(Math.round(number / 10) * 10);

提交回复
热议问题