Summary of Math methods in JavaScript

1. Math.round(x): rounding

The round() method rounds a number to the nearest integer.

  • x  -- Required, must be a number.

First decimal place < 5

Positive numbers: Math.round(11.48) = 11

Negative numbers: Math.round(-11.48) = -11

First decimal place = 5

Positive numbers: Math.round(11.5) = 12

Negative numbers: Math.round(-11.5) = -11

First decimal place > 5

Positive numbers: Math.round(11.78) = 12

Negative numbers: Math.round(-11.78) = -12

2. Math.ceil(x): round up

The ceil() method performs an upward rounding calculation, which returns the nearest integer that is greater than or equal to the function parameter.

If the passed parameter is an integer, the value is unchanged.

  • x  -- Required, must be a number.

Math.ceil(11.48)   =  Math.ceil(11.68)  = Math.ceil(11.5)    = 12

Math.ceil(-11.48)  =  Math.ceil(-11.68) = Math.ceil(-11.5)   = -11

3. Math.floor(x): round down

The floor() method returns the largest integer less than or equal to x. If the passed parameter is an integer, the value is unchanged.

  • x  -- Required, must be a number.

Math.ceil(11.48)   =  Math.ceil(11.68)  = Math.ceil(11.5)    = 11

Math.ceil(-11.48)  =  Math.ceil(-11.68) = Math.ceil(-11.5)   = -12

4、Math.max(n1,n2,n3,...,nX)

The max() method returns the greater of two specified numbers.

  • n1,n2,n3,...,nX -- optional. 1 or more values. Prior to ECMASCript v3, this method had only two parameters.

return value

  • Number: The largest value in the parameter. If no arguments, returns -Infinity. Returns NaN if any of the arguments is NaN, or a non-numeric value that cannot be converted to a number.

Math.max(5,10); // returns 10

Math.max(0,150,30,20,38); // returns 150

Math.max(-5,-10); // returns -5

Math.max(2.5, 3.5); // returns 3.5

5、Math.min( n1 , n2 , n3 ,...,nX)

The min() method returns the number that has the smallest value of the two specified numbers.

  • n1,n2,n3,...,nX -- optional. 1 or more values. Prior to ECMASCript v3, this method had only two parameters.

return value

  • Number: The smallest value among the parameters. If no arguments, returns -Infinity. Returns NaN if any of the arguments is NaN, or a non-numeric value that cannot be converted to a number.

Math.min(5,10); // returns 5

Math.min(0,150,30,20,38); // returns 0

Math.min(-5,-10); // returns -10

Math.min(2.5, 3.5); // returns 2.5

Guess you like

Origin blog.csdn.net/loveliqi/article/details/125482790