How to Round Down a Number in JavaScript

avatar

Borislav Hadzhiev

Wed Oct 27 20211 min read

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.

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

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

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee