language

1339Currently online
62Today's Reading
48Share Today
Multilingual display

Math.floor(), Math.round(), Math.ceil()

2018-05-06 09:36:05

In javascript we often use Math.floor(), Math.round(), Math.ceil(). Let's take a look at these three methods

Tools/Materials

js Math.round() Math.floor() Math.ceil()

Methods/Steps
1

Math.floor(), Math.round() methods perform integer calculations, they get the nearest integer and return your NaNMath.floor()NaNMath.round()NaN when the argument is not present

2

Let's pass in the other parameters using Math.floor(), as shown below. And you can see that everything that comes back is smaller than that, The nearest integer Math.floor(2.9)2Math.floor(-2.9)-3Math.floor(-0.5)-1Math.floor(-0.9)-1Math.floor(0.9)0Math.floor(1.9)1

3

The Math.round() method rounds a number to the nearest number. As you can see, Math.round() is used in a similar way to the way we normally round, It returns the nearest integer Math.round(1.2)1Math.round(1.6)2Math.round(-1.6)-2Math.round(0.9)1Math.round(0.49)0Math.round(0.01)0Math.rou Nd (0.01) - 0 math. Round (0.4) - 0

4

The ceil() method is rounded up and returns an integer greater than or equal to the argument. Math.ceil() returns NaNMath.ceil()NaN without the argument

5

ceil() method is calculated by rounding up, it is actually the opposite of floor, below we pass in some parameters to look at the method, you can see that the return is larger than it. The nearest integer Math.ceil()NaNMath.ceil(1.2)2Math.ceil(-1.2)-1Math.ceil(22.10)23Math.ceil(77.10)78Math.ceil(76.9999)77Math.ceil(- 76.9999) - 76

Matters needing attention

Note the difference between Math.floor() and Math.ceil()

Recommendation