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.
round to Round a Number to the Nearest 10 in JavaScript. To round a number to the nearest 10 , you can call the Math. round() function, passing the number divided by 10 and multiplying the result by 10 , e.g., Math. round(num / 10) \* 10 .
The Math. round() method rounds a number to the nearest integer. 2.49 will be rounded down (2), and 2.5 will be rounded up (3).
ceil() The Math. ceil() function always rounds a number up to the next largest integer.
Divide the number by 10, round the result and multiply it with 10 again:
var number = 33; console.log(Math.round(number / 10) * 10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With