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

Wed Oct 27 20212 min read

Photo by Henri Pham

## Round a Number to the Nearest 100#

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

index.js
```Copied!```function roundNearest100(num) {
return Math.round(num / 100) * 100;
}

console.log(roundNearest100(349)); // ๐๏ธ 300
console.log(roundNearest100(350)); // ๐๏ธ 400
console.log(roundNearest100(175.1)); // ๐๏ธ 200
console.log(roundNearest100(-351)); // ๐๏ธ -400
console.log(roundNearest100(-350)); // ๐๏ธ -300
``````

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

The main piece of the puzzle is the Math.round function. 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(6.49)); // ๐๏ธ 6
console.log(Math.round(6.5)); // ๐๏ธ 7
console.log(Math.round(60)); // ๐๏ธ 60
console.log(Math.round(-64.5)); // ๐๏ธ -64
console.log(Math.round(-64.51)); // ๐๏ธ -65

console.log(Math.round(null)); // ๐๏ธ 0
``````
If the `Math.round` function is called with a `null` value, it returns `0`.

Let's go over the example in a step-by-step manner.

index.js
```Copied!```console.log(250 / 100); // ๐๏ธ 2.5
console.log(440 / 100); // ๐๏ธ 4.4

console.log(Math.round(250 / 100)); // ๐๏ธ 3
console.log(Math.round(440 / 100)); // ๐๏ธ 4

console.log(Math.round(250 / 100) * 100); // ๐๏ธ 300
console.log(Math.round(440 / 100) * 100); // ๐๏ธ 400
``````

The code consists of 2 steps:

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