preface

Math => ‘object’ Typeof Math => ‘object’

Console. dir(Math) View all function methods of Math.

Math common functions

Math.abs() gets the absolute value

Math.abs(-12) = 12
Copy the code

Math.ceil() and math.floor () round up and down

The console. The log (math.h ceil (12.03)); / / 13 console. The log (math.h ceil (12.92)); / / 13 console. The log (math.h floor (12.3)); / / 12 console. The log (math.h floor (12.9)); / / 12Copy the code

Math.round() is rounded

Note: positive numbers including 5 are rounded up, negative numbers including 5 are rounded down.

1, Math. Round (16.3) = 2-16, Math, round (16.5) = - 16 3, Math. Round (16.51) = 4-17, Math. The random () in [0, 1), the random decimalCopy the code

Case 1: get a random integer [0,10]

console.log(parseInt(Math.random()*10)); // Does not contain 10 console.log(parseInt(math.random ()*10+1)); / / contains 10Copy the code

Case 2: Get random integers between [n,m]

Math.round(Math.random()*(m-n)+n)
Copy the code

Math.max() and max.min () get the maximum and minimum values in a set of data

The console. The log (Math. Max (10,1,9,100,200,45,78)); The console. The log (Math. Min (10,1,9,100,200,45,78));Copy the code

Math.pi retrieves the value of PI

console.log(Math.PI);

Math.pow() and Math.sqrt()

Math.pow() takes a value to the number of powers

Math.pow(10,2) = 100;
Copy the code

Math.sqrt() takes the square root of the value

Math.sqrt(100) = 10;
Copy the code