How to Round Down a Number in JavaScript


Borislav Hadzhiev

Last updated: Oct 27, 2021


Photo from Unsplash

Round Down a Number in JavaScript #

To round down a number, pass it to the Math.floor() function, e.g. Math.floor(5.5). The Math.floor function returns a number that represents the largest integer that is less than or equal to the provided number.

console.log(Math.floor(3.99)); // 👉️ 3 console.log(Math.floor(3.009)); // 👉️ 3 console.log(Math.floor(-3.01)); // 👉️ -4 console.log(Math.floor(-3.99)); // 👉️ -4 console.log(Math.floor(null)); // 👉️ 0

We used the Math.floor function to round a number down.

The function rounds numbers with a fractional part to the largest integer that is less than the passed in number.

If the number is an integer, it gets returned as is.

console.log(Math.floor(5)); // 👉️ 5 console.log(Math.floor(-55)); // 👉️ -55

In other words, the function rounds towards negative infinity.

Note that the Math.floor function returns 0 if you pass it a null value.

Further Reading #

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.