Math.floor() Method in JS – How to Round Down Numbers
Whenever you invoke Math.floor(), the method rounds down its argument to the nearest integer.
note
Math.floor()
is one of the built-in methods of the globalMath
object.- The global
Math
object is one of the standard built-in JavaScript objects.
Syntax of the Math.floor()
Method
Math.floor()
accepts only one argument. Here is the syntax:
Math.floor(value);
The value
argument represents the number you wish to floor (round down) to the nearest integer.
note
- Suppose the
value
argument is already an integer. In that case,Math.floor()
will return the same number. Math.floor()
is equivalent to-Math.ceil(-value)
.
Examples
Below are examples of the Math.floor()
method.
How to round down 9
to the nearest integer
Math.floor(9);
// The invocation above will return: 9
How to round down 9.999
to the nearest integer
Math.floor(9.999);
// The invocation above will return: 9
How to round down -9.999
to the nearest integer
Math.floor(-9.999);
// The invocation above will return: -10
The snippet above returned -10
because -10
is the integer directly below -9.999
on the number line. In other words, -9
is greater than -9.999
. But -10
is less than -9.999
.
How to round down 0.91
to the nearest integer
Math.floor(0.91);
// The invocation above will return: 0
CodeSweetly TIP
- To round up a number, use
Math.ceil()
. - To generate random numbers, use
Math.random()
.
Overview
This article discussed what JavaScript's Math.floor()
method does. We also used examples to see how it works.