Round a Number (up or down) to the Nearest 10 in JS

Borislav Hadzhiev

Wed Oct 27 2021·2 min read

Photo by Greg Rakozy

**To round a number to the nearest 10, call the Math.round() function, passing
it the number divided by 10 and multiply the result by 10, e.g.
Math.round(num / 10) * 10. The Math.round function takes a number, rounds it
to the nearest integer and returns the result.**

index.js

`function roundToNearest10(num) { return Math.round(num / 10) * 10; } console.log(roundToNearest10(24)); // 👉️ 20 console.log(roundToNearest10(25)); // 👉️ 30 console.log(roundToNearest10(54.9)); // 👉️ 50 console.log(roundToNearest10(-26)); // 👉️ -30 console.log(roundToNearest10(-25)); // 👉️ -20`

If you only need to round UP to the nearest 10, check out my other article - Round a Number Up to the Nearest 10 in JavaScript.

The Math.round function took care of the heavy lifting for us.

The function rounds a number to the nearest integer.

If the number has a fractional portion of exactly

`0.5`

, the number gets rounded to the next integer in the direction of positive infinity.Here are some examples of using the `Math.round`

function.

index.js

`console.log(Math.round(9.49)); // 👉️ 9 console.log(Math.round(9.5)); // 👉️ 10 console.log(Math.round(90)); // 👉️ 90 console.log(Math.round(-94.5)); // 👉️ -94 console.log(Math.round(-94.51)); // 👉️ -95 console.log(Math.round(null)); // 👉️ 0`

If you invoke the

`Math.round`

function with a `null`

value, it returns `0`

.Here is the example, covered step-by-step.

index.js

`console.log(75 / 10); // 👉️ 7.5 console.log(54 / 10); // 👉️ 5.4 console.log(Math.round(75 / 10)); // 👉️ 8 console.log(Math.round(54 / 10)); // 👉️ 5 console.log(Math.round(75 / 10) * 10); // 👉️ 80 console.log(Math.round(54 / 10) * 10); // 👉️ 50`

This is a two step process:

- Divide the number by
`10`

and round the result to the nearest integer. - Multiply the result by
`10`

to get the number rounded to the nearest`10`

.

Use the search field on my Home Page to filter through my more than 1,000 articles.