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

Wed Oct 27 20212 min read

Photo by Greg Rakozy

Round a Number to the Nearest 10#

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
```Copied!```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
```Copied!```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
```Copied!```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:

1. Divide the number by `10` and round the result to the nearest integer.
2. Multiply the result by `10` to get the number rounded to the nearest `10`.