Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

like image 409
Abs Avatar asked Nov 05 '09 22:11

Abs


People also ask

How do you round to the nearest 10 in JavaScript?

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 .

How do you round up and down in JavaScript?

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).

How do you round up in JavaScript?

ceil() The Math. ceil() function always rounds a number up to the next largest integer.


1 Answers

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

var number = 33;  console.log(Math.round(number / 10) * 10);
like image 152
Gumbo Avatar answered Oct 22 '22 21:10

Gumbo