javascript built-in objects of -Math

Math object

Math object name suggests is a mathematical object, which is mainly used to perform some mathematical tasks to facilitate people to computing, which generally belongs to a function, it is generally can be used directly.

A data type. : Object type is the Math object

console.log(typeof Math);//object

II. Functions

1.Math.round : This function is main role is to allow data taking integer values nearest
Example:

//  方式:小数值通过四舍五入的方法来取整
//	参数类型:数值 ,传字符时得到的结果是NaN
//  参数个数:任意个,但是只有第一个参数参与函数
//  返回值:执行函数后的结果
    console.log(Math.round(2.585.66,7.11)); //3
    // 规范格式
    console.log(Math.round(2.58)); //3
	console.log(Math.round(2.11)); //2
//  不是数值时
    console.log(Math.round("a"));  //NaN

2.Math.random : This function is to take a primary role in the random number between [0, 1), must pay attention to: the value of the random number takes 0 to 1 but not eligible ;

//	参数类型:
//  参数个数:不需要传参
//  返回值:执行函数后的结果
//  规范格式
 console.log(Math.random());//[0,1)之间的数

expand: There are some small partners might want to ask, I would like to take some of the greater range of random values, then how can I do it? For example, I would like to take a range of 24-30 this number it?

Now I will introduce a wrapper function to help you solve this problem!

// 封装范围随机数的功能函数
    function random(a,b){
        return Math.round(Math.random()*(a-b)+b);//注意我们用Math.round是为了让随机的值为整数
    }
//   所以当你需要某个范围的随机数时,只需执行此函数,并且给它传参就可以啦
    var r = random(24,30); //24到30之间的随机数
    console.log(r);

3.Math.ceil : This function is very similar to the role and Math.round are acting on rounding, but where does the difference, the difference is that this function returns the rounded up, no matter how many decimal places will be taken into a whole operation.

//	参数类型:数值
//  参数个数:任意个,但是只有第一个参数参与函数
//  返回值:执行函数后的结果
console.log(Math.ceil(1.012.11,3.55)); //2
//  规范格式
console.log(Math.ceil(1.01)); //2
console.log(Math.ceil(1.99)); //2

4.Math.floor : Gang finished Math.ceil we know that this function is mainly applied to round up, since it has rounded up, and that there must be rounded down to it, yes, we Math.floor role It is rounded down.

//	参数类型:数值
//  参数个数:任意个,但是只有第一个参数参与函数
//  返回值:执行函数后的结果
console.log(Math.floor(6.535.11,4.33)); //6
//  规范格式
console.log(Math.floor(6.53)); //6
console.log(Math.floor(7.11)); //7

5.Math.min : min see the English word, we can expect a minimum. Does it function in connection therewith, yes, this method is mainly used to find the minimum value from a plurality of parameters!

//	参数类型:数值
//  参数个数:任意个
//  返回值:执行函数后的结果(最小值)
//  规范格式
console.log(Math.min(1,3,5,6,7,2,11,8,35)); //1

6.Math.max : min Since it is a minimum, the name suggests that max is the greatest, so this function is mainly to find out the maximum from the plurality of parameters!

//	参数类型:数值
//  参数个数:任意个
//  返回值:执行函数后的结果(最大值)
//  规范格式
console.log(Math.max(1,3,5,6,7,2,11,8,35)); //35

note:
Min and max are received plurality of parameters, not an array, an array can not be received, forced write, get NaN
Example:

console.log(Math.min([34,56,37,24,89,13])); //NaN
console.log(Math.max([34,56,37,24,89,13])); //NaN

7.Math.pow : This function is the main operation of the power equation there;

//	参数类型:数值
//  参数个数:任意个,但是只以前两个参数为准
// 第一个参数属于底数,第二个参数属于次方数
//  返回值:执行函数后的结果
   console.log(Math.pow(2,3,7,8)) //8
// 规范格式
   console.log(Math.pow(2,3)) //8

8.Math.sqrt : Since there function computing power, it must be a function of the square root of the right, we sqrt function main role is to open the square. Note thatsquare root! square root! square root!

//	参数类型:数值
//  参数个数:任意个,但是只有第一个参数参与函数
//  返回值:执行函数后的结果
console.log(Math.sqrt(9,4,25))//3
//  规范格式
 console.log(Math.sqrt(9))//3

9.math.sin and Math.cos : I believe that many small partners met to calculate trigonometric formulas are a headache on right, here we introduce these two functions in the programming you can put your questions to solve .

// sin和cos方法接收的是弧度,不是角度
// 90的角度
//  返回值:执行函数后的结果
//  规范格式
    console.log(Math.sin( Math.PI / 180 * 90 ));
    console.log(Math.cos( Math.PI / 180 * 90 ));

I believe the sharp-eyed students have seen a correlation Math equation right, right, you are very careful this formula is Math.PI; that you may be wondering this is doing with it, here we come to introduce Math .PI.

10.Math.PI : Math.PI property returns the values of π mathematical constant pi.

//	参数类型:
//  参数个数:没有
//  返回值:执行函数后的结果
var pi = Math.PI;
    console.log(pi); //3.14159265358...

11.Math.abs : The main role of this function is the absolute value

//	参数类型:数值
//  参数个数:任意个,但是只有第一个参数参与函数
//  返回值:执行函数后的结果
console.log(Math.abs(10,9,4)); //10
//  规范格式
console.log(Math.abs(10)); //10
console.log(Math.abs(-12)); //12

These are the Math object introduced me to everyone as well as some of its functions and their role in the desire to help you solve some of your problems, I hope you remember to keep in mind cooked, after all, our future programming need them but a lot of Oh!

Published 15 original articles · won praise 10 · views 496

Guess you like

Origin blog.csdn.net/weixin_43797492/article/details/104461108