Math 常用方法
Math is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object.
Math works with the Number type. It doesn’t work with BigInt.
Description
Unlike many other global objects, Math is not a constructor. All properties and methods of Math are static. You refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method’s argument. Constants are defined with the full precision of real numbers in JavaScript.
Static Properties
Math.PI
Ratio of a circle’s circumference to its diameter; approximately 3.14159.
Static Methods
Math.abs(x)
Returns the absolute value of x.
1 | Math.abs(-0.99) // 0.99 |
Math.ceil(x)
Returns the smallest integer greater than or equal to x.
1 | Math.ceil(0.1) // 1 |
Math.floor(x)
Returns the largest integer less than or equal to x.
1 | Math.floor(0.9) // 0 |
Math.round(x)
Returns the value of the number x rounded to the nearest integer.
1 | Math.round(0.49) // 0 |
Math.max([x[, y[, …]]])
Returns the largest of zero or more numbers.
1 | Math.max(1, 2, 3, 3.15, Math.PI) // 3.15 |
Math.min([x[, y[, …]]])
Returns the smallest of zero or more numbers.
1 | Math.min(0, -1) // -1 |
Math.pow(x, y)
Returns base x to the exponent power y (that is, x^y).
1 | Math.pow(2, 3) // 8 |
Math.random()
Returns a pseudo-random number between 0 and 1.
1 | // Returning a random integer between two bounds |