如何在 Javascript 中将整数四舍五入到最接近的百位?

How to round an integer to the nearest hundred in Javascript?

假设我有一个号码

let x = 890

我希望它四舍五入到 900

如果数字是 820,则应四舍五入为 800。

我们还假设输入数字 (x) 始终是整数,

我知道我们可以通过手动检查它是否大于 50 来解决这个问题,但是有没有更简单的解决方案?最好通过 javascript.

中的递归

如果要上下舍入

Math.round(number/100)*100

如需更深入了解并与 Math.ceil() 进行比较,请查看此 link:

How to round up to the nearest 100 in JavaScript

就在我的头顶:

取任意数字,除以百,用 Math.round() 四舍五入,乘以 100 得到您想要的数字。

const roundNumber = (num) => Math.round(num/100) *100