When working with large amounts of data, there are times when you will come across an array of numbers, which you would then have to manipulate to gather and analyze information from. The great thing is that Javascript comes with a built-in Math object which has many properties that will come in handy in many situations! I will be talking about a couple of functions that hopefully will help you for future use.
当处理大量数据时,有时会遇到一系列数字,然后必须操纵这些数字来收集和分析其中的信息。 很棒的事情是Javascript带有一个内置的Math对象,该对象具有许多可以在许多情况下派上用场的属性! 我将讨论一些功能,希望这些功能可以帮助您将来使用。
Math.max()
Math.max()
The Math.max()
function returns the greatest number in an array, all you must do is pass it inside the parenthesis, but when you using a variable as an array, you must pass in the spread operator before it like so:
Math.max()
函数返回数组中的最大数,您所要做的就是将其传递到括号内,但是当您将变量用作数组时,必须像这样先传递散布运算符:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/b79a388289e8266c65ac2840e11354ac.png)
Math.min()
Math.min()
The Math.min()
function is similar to the max function, but instead returns the lowest number in an array, and just like the max, when you using a variable as an array, you must pass in the spread operator before it
Math.min()
函数与max函数类似,但是返回的是数组中的最小数字,就像max一样,当您将变量用作数组时,必须在其之前传入散布运算符
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/12c6efc5d58ee650caef34f40459eeb7.png)
Math.round()
Math.round()
The Math.round()
function returns the value of a number rounded to its nearest integer:
Math.round()
函数返回四舍五入至最接近整数的数字的值:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/83119d5b67576b0a786df2ac4278725d.png)
Math.pow()
Math.pow()
The Math.pow(x, y)
function, takes in two parameters, which returns the value of x to the power of y:
Math.pow(x, y)
函数接受两个参数,这些参数将x的值返回为y的幂:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/b137e2b8c294dcb72f2d4ea252349e22.png)
Math.sqrt()
Math.sqrt()
The Math.sqrt()
function returns the square root of any given positive number:
Math.sqrt()
函数返回任何给定正数的平方根:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/9d3c53014d64910af2916222933a730f.png)
Math.abs()
Math.abs()
The Math.abs()
function returns the absolute value of any number, which basically turns it into a positive number and can also be useful when using to find the difference without return a negative number:
Math.abs()
函数返回任何数字的绝对值,这基本上将其转换为正数,并且在用于查找差异而不返回负数时也很有用:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/0062db079b5ce354a4510747872f9a10.png)
Math.PI
数学PI
The Math.PI
function calculates the 16 digits of pi (π):
Math.PI
函数计算pi(π)的16位数字:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/b09b10be6c507331ededcf2da6555367.png)
These are just some of the many functions that the Math object has, although they may be a simple, getting to know these and practicing will give you an upper edge when working with integers and when solving arithmetic problems.
这些只是Math对象具有的许多功能中的一部分,尽管它们可能很简单,但了解它们并进行实践将在处理整数和解决算术问题时为您提供优势。
翻译自: https://levelup.gitconnected.com/useful-javascript-math-functions-d1016478f0f2