Using Math.floor() is one way of doing this.

More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

Answer from phoebus on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN - Mozilla
The Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.
🌐
W3Schools
w3schools.com › jsref › jsref_floor.asp
JavaScript Math floor() Method
❮ Previous JavaScript Math Object ... Math.floor(-5.1); let f = Math.floor(-5.9); Try it Yourself » · The Math.floor() method rounds a number DOWN to the nearest integer....
Discussions

Why not always use Math.round instead of Math.floor?

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

More on reddit.com
🌐 r/javascript
13
1
February 22, 2017
This is why you should not using "toFixed" to rounding a number to the nearest integer.
The specification https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.prototype.tofixed is just incomprehensible ... More on reddit.com
🌐 r/JavaScriptTips
4
10
July 9, 2022
what is the difference between using math.ceil and round for rounding the decimal ?
ceil always rounds up. More on reddit.com
🌐 r/learnpython
3
2
October 10, 2022
Round up to the nearest hundred
May be m*math.ceil((N+m-1)/m) , round N up to next multiple of m. Not tested as on phone. Added: probably similar to hawhill, misunderstood [nearest] :) may fix later. More on reddit.com
🌐 r/lua
7
3
August 5, 2022
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN - Mozilla
console.log(Math.round(0.9)); // Expected output: 1 console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05)); // Expected output: 6 6 5 console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95)); // Expected output: -5 -5 -6
🌐
SheCodes
shecodes.io › athena › 3104-javascript-math-round-down-function-explained
[JavaScript] - JavaScript Math Round Down Function | SheCodes
Discover how to use `Math.floor()` function in JavaScript to return the largest integer less than or equal to a given number.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › floor
JavaScript Math floor() - Round Down Value | Vultr Docs
November 29, 2024 - The Math.floor() function in JavaScript is a crucial tool for rounding down numerical values to the nearest whole number.
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_round.asp
JavaScript Math round() Method
❮ Previous JavaScript Math Object ... Math.round(-2.50); let f = Math.round(-2.49); Try it Yourself » · The Math.round() method rounds a number to the nearest integer....
🌐
pawelgrzybek
pawelgrzybek.com › rounding-and-truncating-numbers-in-javascript
Rounding and truncating numbers in JavaScript | pawelgrzybek.com
January 19, 2016 - Rounding is straight forward. We can round to the nearest integer, round down or round up. JavaScript uses three methods to achieve this: Math.round() - rounds to the nearest integer (if the fraction is 0.5 or greater - rounds up)
🌐
CoreUI
coreui.io › answers › how-to-round-a-number-in-javascript
How to round a number in JavaScript · CoreUI
September 26, 2025 - For rounding down use Math.floor(), for rounding up use Math.ceil(). Consider using Intl.NumberFormat for locale-aware number formatting in international applications. ... Follow Łukasz Holeczek on GitHub Connect with Łukasz Holeczek on LinkedIn ...
🌐
SitePoint
sitepoint.com › blog › javascript › a guide to rounding numbers in javascript
A Guide to Rounding Numbers in JavaScript — SitePoint
November 13, 2024 - We can use Math.fround to see how it’s actually represented: ... As you can see, it’s actually represented by the floating point number 3.549999952316284, which rounds down to 3.5. These problems with rounding numbers in JavaScript don’t occur too often, but they’re definitely something you should be aware of if you’re doing a lot of rounding — especially when it’s important that the result is accurate.
🌐
Code Boxx
code-boxx.com › home › round off in javascript (up, down, to decimal places)
How To Round Off In Javascript (Up, Down, To Decimal ...
August 7, 2024 - As in the introduction, there are 4 native Javascript Math functions to round off numbers: Math.round() rounds off to the nearest whole number. That is, decimals with less than 0.5 will be rounded down, rounded up if more than or equals to 0.5.
🌐
TutorialsPoint
tutorialspoint.com › how-can-i-round-down-a-number-in-javascript
How can I round down a number in JavaScript?
July 14, 2022 - This tutorial teaches us to round down a number in JavaScript. The meaning of the round-down number is “returning the integer which is equal to or less than the current float number”.For example, if we round down the number 9.99, we get 9 as an outpu
🌐
GoLinuxCloud
golinuxcloud.com › home › javascript › how to round down a number javascript [solved]
How to round down a number JavaScript [SOLVED] | GoLinuxCloud
November 12, 2022 - So, if the decimal point of the number is greater than or equal to 0.5, it rounds up to the next higher absolute value, but if is lower than 0.5, it rounds down to the lower absolute value.
🌐
Math.js
mathjs.org › docs › reference › functions › round.html
math.js | an extensive math library for JavaScript and Node.js
math.round(3.22) // returns number 3 math.round(3.82) // returns number 4 math.round(-4.2) // returns number -4 math.round(-4.7) // returns number -5 math.round(3.22, 1) // returns number 3.2 math.round(3.88, 1) // returns number 3.9 math.round(-4.21, 1) // returns number -4.2 math.round(-4.71, 1) // returns number -4.7 math.round(math.pi, 3) // returns number 3.142 math.round(123.45678, 2) // returns number 123.46 const c = math.complex(3.2, -2.7) math.round(c) // returns Complex 3 - 3i const unit = math.unit('3.241 cm') const cm = math.unit('cm') const mm = math.unit('mm') math.round(unit, 1, cm) // returns Unit 3.2 cm math.round(unit, 1, mm) // returns Unit 32.4 mm math.round([3.2, 3.8, -4.7]) // returns Array [3, 4, -5]
🌐
CodeParrot
codeparrot.ai › blogs › javascript-round-to-2-decimal-places-a-complete-guide
JavaScript Round to 2 Decimal Places: A Complete Guide
JavaScript's floating-point arithmetic can cause unexpected results, especially in JavaScript Round to 2 Decimal Places. Adding Number.EPSILON—the smallest possible value that can be added to 1 to yield a result greater than 1—helps mitigate these errors. const roundWithEpsilon = (num) => Math.round((num + Number.EPSILON) * 100) / 100; console.log(roundWithEpsilon(1.005)); // Output: 1.01 console.log(roundWithEpsilon(1.255)); // Output: 1.26
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-math-round-function-2
JavaScript Math.round( ) function | GeeksforGeeks
May 22, 2023 - The Math.floor() method in JavaScript is used to round off a number down to the nearest integer, moving towards the lower value.
🌐
Reddit
reddit.com › r/javascript › why not always use math.round instead of math.floor?
r/javascript on Reddit: Why not always use Math.round instead of Math.floor?
February 22, 2017 -

When I read through the code of colleagues and public repos, I see Math.floor used like 20x more often than Math.round.

But why? Isn't Math.round more accurate than Math.floor? Shouldn't it be the other way around (using Math.round more often than Math.floor)?

Is Math.floor so much faster than Math.round or am I missing something?

Edit

I am aware that those two do different things. My point is that in my experience, Math.floor is much too often used, when Math.round would simply be more accurate.

Top answer
1 of 6
9

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

2 of 6
3

Math.floor - You have a rating system of stars, and you aren't breaking them up into half stars. You do a query to get all the votes and the math comes back to 4.7 stars. You would use Math.floor here so that you display 4 stars.

 

Math.ceil - You have a slider module that displays 3 slides at a time. This module contains 19 slides. 19/3 = 6.33. If you were to floor or round here you would end up with 6. But to make sure that all 19 slides are shown, you need 7 containers, so you use Math.ceil.

 

Math.round - Anytime you need the closest number without worrying about anything like the above scenarios.

🌐
Educative
educative.io › answers › mathceil-mathfloor-and-mathround-in-javascript
Math.ceil, Math.floor, and Math.round in JavaScript
Math.round() rounds off a number depending on the fractional part of the number. So, if the fractional part is >=.5, it’ll return the smallest integer that is still greater than the passed value.