Last updated: Mar 3, 2024
Reading timeยท8 min
Make sure to click on the correct subheading depending on if you need to round to the nearest
5
,10
or100
.
To round a number to the nearest multiple of 5:
Math.round()
method with the result of dividing the number by 5
.5
.Math.round()
method takes a number and rounds it to the nearest
integer.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
Math.ceil
method from the next subheading.The Math.round() method takes a number and rounds it to the nearest integer.
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.
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
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.
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:
5
and round the result to the nearest integer.5
to get the number rounded to the nearest 5
.The Math.round()
function handles all the heavy lifting for us.
The process of rounding up to the next multiple of 5 is:
Math.ceil()
method with the result of dividing the number by 5
.5
.Math.ceil()
method takes a number and rounds it to the nearest integer.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.
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.
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:
5
and round the result up, to the nearest integer.5
to get the number rounded up to the next multiple
of 5
.You can use the Math.floor()
method to always round the number down to the
nearest multiple of 5
.
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.
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.
To round a number to the nearest multiple of 10:
10
to the Math.round()
function.10
.Math.round()
function takes a number and rounds it to the nearest
integer.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.
0.5
, it gets rounded to the next higher absolute value.Here are some examples of using the Math.round()
method.
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
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.
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:
10
and round the result to the nearest integer.10
to get the number rounded to the nearest 10
.The Math.round()
function handles all the heavy lifting for us.
The process of rounding up/down to the nearest 10 is:
Math.ceil()
function to round the number up to the nearest multiple
of 10.Math.floor()
function to round the number down to the nearest
multiple of 10.// โ 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.
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.
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
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.
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:
10
and round the result up to the next largest
integer.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.
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.
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
null
value to the Math.floor
function, it returns 0
.Here is the step-by-step example from the code snippet.
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:
10
and round the result down to the next largest
integer.10
to get the number rounded down to the nearest
10
.To round a number to the nearest multiple of 100:
100
to the Math.round()
function.100
.Math.round()
function takes a number and rounds it to the nearest
integer.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.
0.5
, it gets rounded to the next higher absolute value.Here are some examples of using the Math.round()
method.
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
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.
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:
100
and round the result to the nearest integer.100
to get the number rounded to the nearest 100
.The Math.round()
function handles all the heavy lifting for us.
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.
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.
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.
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
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
.
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:
100
and round the result up to the next largest
integer.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.
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.
console.log(Math.floor(7.99999)); // ๐๏ธ 7 console.log(Math.floor(7.00001)); // ๐๏ธ 7
You can learn more about the related topics by checking out the following tutorials: