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

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
8 min

banner

# Table of Contents

  1. Round a Number to the Nearest 5 in JavaScript
  2. Round a Number to the Nearest 10 in JavaScript
  3. Round a Number to the Nearest 100 in JavaScript

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

round number to nearest multiple of 5

The code for this article is available on GitHub
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
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

examples of using math round

The code for this article is available on GitHub
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
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
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

round number up to next multiple of 5

The code for this article is available on GitHub

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

index.js
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
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
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

round number down to nearest multiple of 5

The code for this article is available on GitHub

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

index.js
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.

# Table of Contents

  1. Round a Number to the Nearest 10 in JavaScript
  2. Round a Number to the Nearest 100 in JavaScript

# 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
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 code for this article is available on GitHub

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
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
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
// โœ… 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 code for this article is available on GitHub

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
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
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
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 code for this article is available on GitHub

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
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
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
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 code for this article is available on GitHub

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
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
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 for this article is available on GitHub

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
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 for this article is available on GitHub

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
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
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
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
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 code for this article is available on GitHub

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

index.js
console.log(Math.floor(7.99999)); // ๐Ÿ‘‰๏ธ 7 console.log(Math.floor(7.00001)); // ๐Ÿ‘‰๏ธ 7

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev