Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Math is a built-in object in JavaScript that provides mathematical properties and mathematical function methods to evaluate numbers. It is used for the Number type.

What is Math?

Unlike other global objects, Math is not a constructor. All properties and methods of Math are static. If you want to call its methods. Use and pass in the corresponding arguments, such as math.max (x).

The Math approach is often used in front-end development, so attributes will not be covered in this article

Common Math methods

Math.abs(x)

Returns the absolute value of a number

/ / > Math. Abs (1.9) > 1.9 math.h abs (1) / / 1 > math.h abs / / 0 (0)Copy the code

Math.ceil(x)

Returns the smallest integer greater than or equal to the given number, that is, the rounded value of a number.

Math.h ceil (. 95) / / 1 math.h ceil (4) / / 4 math.h ceil (7.004) / / 8 math.h ceil (7.004) / / - 7 math.h ceil (0.004) / / 0Copy the code

Math.floor(x)

Returns the largest integer less than or equal to the specified number, that is, the rounded value of a number. \

Math. Floor (45.96) / / 45 math.h floor (45.01) / / 45 math.h floor (45) / / 45 math.h floor (45.01) / / - 46 math.h floor (45.96) / / 46Copy the code

Math. Max ([[x, y, [...]]])

Returns the maximum value of a set of numbers.

If there are no arguments, the result is -infinity.

If any of the parameters cannot be converted to a value, the result is NaN.

Math.max(10, 20); // 20 Math.max(-10, -20); // -10 Math.max(-10, 20); Math. Max (null, 2, 3) // 3 math. Max (null, -2, -3) // null is converted to 0: 0 Math.max(undefined, -2, -3) // NaN Math.max('re', 2, 3) // NaN Math.max([99, 32], -2, -3) // NaNCopy the code

Math. Min ([[x, y, [...]]])

Returns the minimum value in a set of numbers.

If there are no arguments, the result is Infinity.

If any of the parameters cannot be converted to a value, the result is NaN.

Math.random()

A pseudo-random floating point number between 0 (inclusive) and 1 (exclusive)

  • Get a random number between greater than or equal to 0 and less than 1
function getRandom() {
  return Math.random();
}
Copy the code
  • Get the random number between any two numbers

Not less than min (possibly equal to), and less than Max (not equal to)

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
Copy the code

Math.round(x)

Returns a rounded integer

Math. Round (20.49); / / 20 Math. Round (20.5); / / 21 math.h round (20.5); Math.round(-20.51); // Math.round(-20.51); / / - 21Copy the code

Math.trunc(x)

Returns the integral part of a number, removing the decimal point and all subsequent parts.

Removes the decimal part and decimal point of a number, regardless of whether the argument is positive or negative.

Arguments passed into the method are implicitly converted to numeric types.

Math.trunc(13.37) // 13 Math.trunc(42.84) // 42 Math.trunc(0.123) // 0 math. trunc(-0.123) // -0 math. trunc("-1.123") // -1 Math.trunc(NaN) // NaN Math.trunc("foo") // NaN Math.trunc() // NaNCopy the code

conclusion

If this article helped you, please like 👍 and follow ⭐️.

If there are any errors in this article, please correct them in the comments section 🙏🙏.