# Round a Number to the Nearest 5, 10 or 100 in JavaScript

Last updated: Mar 3, 2024
8 min

Make sure to click on the correct subheading depending on if you need to round to the nearest `5`, `10` or `100`.

## #Round a Number (up or down) to the Nearest 5 in JavaScript

To round a number to the nearest multiple of 5:

1. Call the `Math.round()` method with the result of dividing the number by `5`.
2. Multiply the result by `5`.
3. The `Math.round()` method takes a number and rounds it to the nearest integer.
index.js
```Copied!```function roundNearest5(num) {
return Math.round(num / 5) * 5;
}

console.log(roundNearest5(12)); // ๐๏ธ 10
console.log(roundNearest5(13)); // ๐๏ธ 15
console.log(roundNearest5(-13)); // ๐๏ธ -15
console.log(roundNearest5(-12)); // ๐๏ธ -10
console.log(roundNearest5(32.4)); // ๐๏ธ 30
console.log(roundNearest5(32.5)); // ๐๏ธ 35
``````

If you need to round UP to the next multiple of 5, use the `Math.ceil` method from the next subheading.

The Math.round() method takes a number and rounds it to the nearest integer.

If the number is positive and its fractional part is greater than or equal to `0.5`, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than `0.5`, it gets rounded to the lower absolute value.

Here are some examples of using the `Math.round()` method.

index.js
```Copied!```console.log(Math.round(4.49)); // ๐๏ธ 4
console.log(Math.round(4.5)); // ๐๏ธ 5
console.log(Math.round(40)); // ๐๏ธ 40
console.log(Math.round(-44.5)); // ๐๏ธ -44
console.log(Math.round(-44.51)); // ๐๏ธ -45

console.log(Math.round(null)); // ๐๏ธ 0
``````

When the `Math.round` function is invoked with a `null` value, it returns `0`.

This is how we round a number to the nearest multiple of five in a step-by-step process.

index.js
```Copied!```console.log(12 / 5); // ๐๏ธ 2.4
console.log(13 / 5); // ๐๏ธ 2.6

console.log(Math.round(12 / 5)); // ๐๏ธ 2
console.log(Math.round(13 / 5)); // ๐๏ธ 3

console.log(Math.round(12 / 5) * 5); // ๐๏ธ 10
console.log(Math.round(13 / 5) * 5); // ๐๏ธ 15
``````

This is a two-step process:

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

The `Math.round()` function handles all the heavy lifting for us.

## #Round a Number UP to the next multiple of 5 in JavaScript

The process of rounding up to the next multiple of 5 is:

1. Call the `Math.ceil()` method with the result of dividing the number by `5`.
2. Multiply the result by `5`.
3. The `Math.ceil()` method takes a number and rounds it to the nearest integer.
index.js
```Copied!```function roundNearest5(num) {
return Math.ceil(num / 5) * 5;
}

console.log(roundNearest5(16)); // ๐๏ธ 20
console.log(roundNearest5(12)); // ๐๏ธ 15
console.log(roundNearest5(11)); // ๐๏ธ 15
console.log(roundNearest5(-13)); // ๐๏ธ -10
console.log(roundNearest5(32.4)); // ๐๏ธ 35
``````

The Math.ceil() method rounds the provided number up returning the smallest integer greater than or equal to the given number.

index.js
```Copied!```console.log(Math.ceil(5.0001)); // ๐๏ธ 6
console.log(Math.ceil(5.999)); // ๐๏ธ 6
``````

This is how we round a number up to the next multiple of five in a step-by-step process.

index.js
```Copied!```console.log(12 / 5); // ๐๏ธ 2.4
console.log(19 / 5); // ๐๏ธ 3.8

console.log(Math.ceil(12 / 5)); // ๐๏ธ 3
console.log(Math.ceil(19 / 5)); // ๐๏ธ 4

console.log(Math.ceil(12 / 5) * 5); // ๐๏ธ 15
console.log(Math.ceil(19 / 5) * 5); // ๐๏ธ 20
``````

This is a two-step process:

1. Divide the number by `5` and round the result up, to the nearest integer.
2. Multiply the result by `5` to get the number rounded up to the next multiple of `5`.

## #Round a number DOWN to the nearest multiple of 5 in JavaScript

You can use the `Math.floor()` method to always round the number down to the nearest multiple of `5`.

index.js
```Copied!```function roundNearest5(num) {
return Math.floor(num / 5) * 5;
}

console.log(roundNearest5(16)); // ๐๏ธ 15
console.log(roundNearest5(12)); // ๐๏ธ 10
console.log(roundNearest5(11)); // ๐๏ธ 10
console.log(roundNearest5(-13)); // ๐๏ธ -15
console.log(roundNearest5(34.6)); // ๐๏ธ 30
``````

The Math.floor() method rounds the number down returning the largest integer that is smaller than or equal to the given number.

index.js
```Copied!```console.log(Math.floor(5.99)); // ๐๏ธ 5
console.log(Math.floor(5.01)); // ๐๏ธ 5
``````

All we had to do was replace the call to the `Math.round()` and `Math.ceil()` methods with `Math.floor()` to round the number down to the nearest multiple of 5.

## #Round a Number to the nearest 10 in JavaScript

To round a number to the nearest multiple of 10:

1. Pass the number divided by `10` to the `Math.round()` function.
2. Multiply the result by `10`.
3. The `Math.round()` function takes a number and rounds it to the nearest integer.
index.js
```Copied!```function roundToNearest10(num) {
return Math.round(num / 10) * 10;
}

console.log(roundToNearest10(71)); // ๐๏ธ 70
console.log(roundToNearest10(79.9)); // ๐๏ธ 80
console.log(roundToNearest10(70.01)); // ๐๏ธ 70
console.log(roundToNearest10(-44)); // ๐๏ธ -40
console.log(roundToNearest10(-46)); // ๐๏ธ -50
``````

The Math.round() method takes a number and rounds it to the nearest integer.

If the number is positive and its fractional part is greater than or equal to `0.5`, it gets rounded to the next higher absolute value.

Here are some examples of using the `Math.round()` method.

index.js
```Copied!```console.log(Math.round(7.49)); // ๐๏ธ 7
console.log(Math.round(4.5)); // ๐๏ธ 5
console.log(Math.round(30)); // ๐๏ธ 30
console.log(Math.round(-34.5)); // ๐๏ธ -34
console.log(Math.round(-34.51)); // ๐๏ธ -35

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

This is how we round a number to the nearest multiple of 10 in a step-by-step manner.

index.js
```Copied!```console.log(21 / 10); // ๐๏ธ 2.1
console.log(40 / 10); // ๐๏ธ 4

console.log(Math.round(21 / 10)); // ๐๏ธ 2
console.log(Math.round(40 / 10)); // ๐๏ธ 4

console.log(Math.round(21 / 10) * 10); // ๐๏ธ 20
console.log(Math.round(40 / 10) * 10); // ๐๏ธ 40
``````

The code consists of 2 steps:

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

The `Math.round()` function handles all the heavy lifting for us.

## #Round a Number UP/Down to the Nearest 10 using Math.ceil and Math.floor

The process of rounding up/down to the nearest 10 is:

1. Use the `Math.ceil()` function to round the number up to the nearest multiple of 10.
2. Use the `Math.floor()` function to round the number down to the nearest multiple of 10.
index.js
```Copied!```// โ Round a number UP to the nearest 10
function roundUpNearest10(num) {
return Math.ceil(num / 10) * 10;
}

console.log(roundUpNearest10(71)); // ๐๏ธ 80
console.log(roundUpNearest10(79.9)); // ๐๏ธ 80
console.log(roundUpNearest10(70.01)); // ๐๏ธ 80
console.log(roundUpNearest10(-49)); // ๐๏ธ -40
console.log(roundUpNearest10(-50)); // ๐๏ธ -50

// ----------------------------------------------

// โ Round a number DOWN to the nearest 10
function roundDownToNearest10(num) {
return Math.floor(num / 10) * 10;
}

console.log(roundDownToNearest10(39)); // ๐๏ธ 30
console.log(roundDownToNearest10(31)); // ๐๏ธ 30
console.log(roundDownToNearest10(399.999)); // ๐๏ธ 390
console.log(roundDownToNearest10(-409)); // ๐๏ธ -410
console.log(roundDownToNearest10(-401)); // ๐๏ธ -410
``````

The first function rounds a number up to the next multiple of 10 and the second rounds a number down to the nearest 10.

When rounding up, the Math.ceil() function handles the heavy lifting for us.

If the passed-in number has a fractional part, the `Math.ceil` function rounds the number up.

If an integer is passed, the function simply returns the number as is.

Here are some examples of using the `Math.ceil()` function.

index.js
```Copied!```console.log(Math.ceil(7.01)); // ๐๏ธ 8
console.log(Math.ceil(71.00001)); // ๐๏ธ 72
console.log(Math.ceil(70)); // ๐๏ธ 70
console.log(Math.ceil(-23.99)); // ๐๏ธ -20

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

Here is a step-by-step process of rounding a number up to the next multiple of 10.

index.js
```Copied!```console.log(21 / 10); // ๐๏ธ 2.1
console.log(40 / 10); // ๐๏ธ 4

console.log(Math.ceil(21 / 10)); // ๐๏ธ 3
console.log(Math.ceil(40 / 10)); // ๐๏ธ 4

console.log(Math.ceil(21 / 10) * 10); // ๐๏ธ 30
console.log(Math.ceil(40 / 10) * 10); // ๐๏ธ 40
``````

There are 2 steps in the code:

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

If you need to round a number down to the nearest multiple of 10, use the `Math.floor()` function.

index.js
```Copied!```function roundDownToNearest10(num) {
return Math.floor(num / 10) * 10;
}

console.log(roundDownToNearest10(39)); // ๐๏ธ 30
console.log(roundDownToNearest10(31)); // ๐๏ธ 30
console.log(roundDownToNearest10(399.999)); // ๐๏ธ 390
console.log(roundDownToNearest10(-409)); // ๐๏ธ -410
console.log(roundDownToNearest10(-401)); // ๐๏ธ -410
``````

The Math.floor() function returns a number that represents the largest integer that is less than or equal to the provided number.

In other words, if the passed-in number has a fractional part, it gets rounded down to the next integer.

And if the number is an integer, the `Math.floor()` function returns the number as is.

Here are some examples of using the `Math.floor()` function.

index.js
```Copied!```console.log(Math.floor(14.9)); // ๐๏ธ 14
console.log(Math.floor(14.1)); // ๐๏ธ 14
console.log(Math.floor(80)); // ๐๏ธ 80
console.log(Math.floor(-14.9)); // ๐๏ธ -15
console.log(Math.floor(-14.1)); // ๐๏ธ -15

console.log(Math.floor(null)); // ๐๏ธ 0
``````
If you pass a `null` value to the `Math.floor` function, it returns `0`.

Here is the step-by-step example from the code snippet.

index.js
```Copied!```console.log(39 / 10); // ๐๏ธ 3.9
console.log(70 / 10); // ๐๏ธ 7

console.log(Math.floor(39 / 10)); // ๐๏ธ 3
console.log(Math.floor(70 / 10)); // ๐๏ธ 7

console.log(Math.floor(39 / 10) * 10); // ๐๏ธ 30
console.log(Math.floor(70 / 10) * 10); // ๐๏ธ 70
``````

The process consists of 2 steps:

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

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

To round a number to the nearest multiple of 100:

1. Pass the number divided by `100` to the `Math.round()` function.
2. Multiply the result by `100`.
3. The `Math.round()` function takes a number and rounds it to the nearest integer.
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
``````

The Math.round() method takes a number and rounds it to the nearest integer.

If the number is positive and its fractional part is greater than or equal to `0.5`, it gets rounded to the next higher absolute value.

Here are some examples of using the `Math.round()` method.

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

This is how we round a number to the nearest multiple of 100 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`.

The `Math.round()` function handles all the heavy lifting for us.

## #Round a Number Up/Down to the Nearest 100 using Math.ceil and Math.floor

You can use the `Math.ceil()` method to round a number up to the next multiple of `100`.

If you need to round a number down to the nearest multiple of `100`, use the `Math.floor()` method.

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

console.log(roundUpToNearest100(101)); // ๐๏ธ 200
console.log(roundUpToNearest100(199)); // ๐๏ธ 200
console.log(roundUpToNearest100(100.001)); // ๐๏ธ 200
console.log(roundUpToNearest100(-399)); // ๐๏ธ -300
console.log(roundUpToNearest100(-301)); // ๐๏ธ -300
``````

The code sample above uses the `Math.ceil()` method to round a number up to the next multiple of 100.

If you need to round a number down to the nearest multiple of `100`, use the `Math.floor()` method instead.

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

console.log(roundDownToNearest100(101)); // ๐๏ธ 100
console.log(roundDownToNearest100(199)); // ๐๏ธ 100
console.log(roundDownToNearest100(100.001)); // ๐๏ธ 100
console.log(roundDownToNearest100(-399)); // ๐๏ธ -400
console.log(roundDownToNearest100(-301)); // ๐๏ธ -400
``````

If the number we passed to the Math.ceil() function has anything after the decimal, the number gets rounded up.

If an integer is passed to the `Math.ceil()` function, it simply returns the number as is.

Here are some examples of using the `Math.ceil()` function.

index.js
```Copied!```console.log(Math.ceil(10.01)); // ๐๏ธ 11
console.log(Math.ceil(19.01)); // ๐๏ธ 20
console.log(Math.ceil(20)); // ๐๏ธ 20
console.log(Math.ceil(-10.99)); // ๐๏ธ -10

console.log(Math.ceil(null)); // ๐๏ธ 0
``````
If you pass a `null` value to the `Math.ceil` function, it returns `0`.

Here are the steps we took to round a number up to the next multiple of `100`.

index.js
```Copied!```console.log(601 / 100); // ๐๏ธ 6.01
console.log(900 / 100); // ๐๏ธ 9

console.log(Math.ceil(601 / 100)); // ๐๏ธ 7
console.log(Math.ceil(900 / 100)); // ๐๏ธ 9

console.log(Math.ceil(601 / 100) * 100); // ๐๏ธ 700
console.log(Math.ceil(900 / 100) * 100); // ๐๏ธ 900
``````

The process consists of 2 steps:

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

The process is the same for rounding a number down to the nearest multiple of `100`, we just have to use the `Math.floor()` method.

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

console.log(roundDownToNearest100(101)); // ๐๏ธ 100
console.log(roundDownToNearest100(199)); // ๐๏ธ 100
console.log(roundDownToNearest100(100.001)); // ๐๏ธ 100
console.log(roundDownToNearest100(-399)); // ๐๏ธ -400
console.log(roundDownToNearest100(-301)); // ๐๏ธ -400
``````

The Math.floor() method rounds the number down returning the largest integer that is smaller than or equal to the given number.

index.js
```Copied!```console.log(Math.floor(7.99999)); // ๐๏ธ 7
console.log(Math.floor(7.00001)); // ๐๏ธ 7
``````