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
The Math.floor() static method always rounds down and returns the largest integer less than or equal to a given number.
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
January 8, 2016
How to Round Down a Number in JavaScript? - TestMu AI Community
How can I javascript round down a number? I know that Math.round() rounds to the nearest decimal, but I need to round down instead. Is there a better way to do this than breaking the number apart at the decimal point and keeping just the integer part? More on community.testmuai.com
🌐 community.testmuai.com
0
September 16, 2024
How do I round DOWN to the nearest 100?
I’m making a levelling system using exp as a percentage of 100. I need a way to get a large amount of Experience (e.g 1250) level up that amount of times (12 times) and have the remaining exp left over (50). To do this I need to round, specifically down, to the nearest 100. More on devforum.roblox.com
🌐 devforum.roblox.com
1
0
May 13, 2024
Perfect example why you have to round/ceil/floor almost every expression with floating points in javascript. This one caused a bug in my game today.
This isn't a javascript thing, this is a floating point numbers thing. Why do we feel the need to blame javascript for this? More on reddit.com
🌐 r/javascript
67
102
June 12, 2015
🌐
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....
🌐
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.
🌐
pawelgrzybek
pawelgrzybek.com › rounding-and-truncating-numbers-in-javascript
Rounding and truncating numbers in JavaScript | pawelgrzybek.com
January 19, 2016 - Rounding down towards negative infinity. Truncating approximates without rounding. In other words, it “rounds” towards zero. Rounding 3.14159 ≈ 3.1416 Truncating 3.14159 ≈ 3.1415 · Hopefully you get the difference. It makes truncating rarely useful in precise calculations (although JavaScript ...
🌐
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
🌐
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?
January 8, 2016 -

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.

🌐
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
🌐
SitePoint
sitepoint.com › blog › javascript › a guide to rounding numbers in javascript
A Guide to Rounding Numbers in JavaScript — SitePoint
November 13, 2024 - Rounding down ensures that the integer will start at zero and that each integer will have an equal chance of being returned. Starting at zero is generally useful, as arrays in JavaScript are zero-indexed, so rounding down will make sure that ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-floor-method
JavaScript Math floor() Method - GeeksforGeeks
November 7, 2024 - The Math.floor() method in JavaScript is used to round off a number down to the nearest integer, moving towards the lower value.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN
If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value.
🌐
Kodeclik
kodeclik.com › javascript-round-down
How to round down a number in Javascript
October 24, 2025 - To round down in Javascript, use Math.floor() to find the largest integer less than or equal to a given number.
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to Round Down a Number in JavaScript? - TestMu AI Community
September 16, 2024 - How can I javascript round down a number? I know that Math.round() rounds to the nearest decimal, but I need to round down instead. Is there a better way to do this than breaking the number apart at the decimal point and…
🌐
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....
🌐
Adobe Support Community
community.adobe.com › home › app communities › acrobat sdk › questions › round down to 0.5
Re: Round down to 0.5
June 22, 2018 - I simply don't have the experience in JavaScript to know that it wouldn't work the same way. I'm not sure I understand your answer. Do you mean that I'd have to list every possible number in order to obtain the value I want? Would I have to do separate lines for 1.1, 1.2, 1.3, 1.4, 1.5, 1.6.1.7, etc.? Right now, what I have is event.value=FLOOR(this.getField("CPE1").value,.5). The CPE1 field is what needs to be rounded. In Excel, the .5 after the comma makes it round down to the nearest .5.
🌐
Markqdavenport
markqdavenport.com › round-down-in-javascript-with-math-floor
Round down in Javascript with Math.floor – Mark Q Davenport
Math.floor is what you use to round numbers down to their nearest whole number. Making it so you end up with an integer.
🌐
Codecademy
codecademy.com › forum_questions › 515cc33c7157aa3a1b000337
math.floor() vs. math.round() | Codecademy
July 24, 2015 - math.floor() rounds the number generated by math.random() down to the last integer, meaning that it would always just round down to 0. Wouldn't math.r...
🌐
MSR
rajamsr.com › home › javascript floor(): round down numbers like a pro
JavaScript Floor(): Round Down Numbers Like a Pro | MSR - Web Dev Simplified
December 9, 2023 - In this example, we have declared a number-type JavaScript variable. I assigned it the value of 3.7. We then call the floor() function on the number variable and assign the result to the result variable.