有用的javascript数学函数

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
Pass in the spread operator(…) or else it will return NaN
传入价差运算符(…),否则它将返回NaN

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

Math.round()

Math.round()

The Math.round() function returns the value of a number rounded to its nearest integer:

Math.round()函数返回四舍五入至最接近整数的数字的值:

Image for post
Round the nearest integer
四舍五入到最接近的整数

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

Math.sqrt()

Math.sqrt()

The Math.sqrt() function returns the square root of any given positive number:

Math.sqrt()函数返回任何给定正数的平方根:

Image for post

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

Math.PI

数学PI

The Math.PI function calculates the 16 digits of pi (π):

Math.PI函数计算pi(π)的16位数字:

Image for post
Returns π
返回π

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值