JavaScript learning second lesson (two)

JavaScript learning second lesson (two)

1. Math object :

This object is used to process mathematical operators. The object does not need to be created manually because it is an attribute of the window object. When the page is loaded, the background automatically creates a Math object, which can be used by window.Math Ali. Or omit the window object and use Math directly.

2. Commonly used attributes of Math-
PI 1. Function: Obtain Pi
2. Format: Math.PI
Insert picture description here
Insert picture description here
3. Commonly used methods of Math

1) Round method:
Function: round the data
Format: Math.round (data);
Return value: return the integer part of the data, that is, this method processes the one digit after the decimal point when rounding the data.

  console.log(Math.round(3.14));

Insert picture description here

2), ceil method:
function:
round up the data, take the smallest integer Format: Math.ceil (data);
return value: the smallest integer larger than the data

  console.log(Math.ceil(3.74));

Insert picture description here
3) Floor method:
Function
: round down the data and take the largest integer Format: Math.floor(data);

console.log(Math.floor(3.74));

4) Random method:
Function: Generate a random number of 0-1, including 0, but not including 1.
Format: Math.random();
Note:
1): If you want to generate nm and include random integers of n and m:

 parseInt(Math.random()*(最大值+1-最小值)+最小值);

2): If you want to generate a random subscript of an array or a random subscript of a string:

 parseInt(Math.random() *数组.length);

        console.log(Math.random());

Insert picture description here
Insert picture description here

5), max method:
function; find the maximum value of a group of data
Format: Math.max (value 1, value 2, value 3...);

6), min method:

Function: Find the minimum value in a group of data
Format: Math.min (value 1, value 2, value 3...);

      console.log(Math.min(1, 2, 4, 7, 10));
        console.log(Math.max(10, 4, 5, 90));

Insert picture description here
7) Abs method:
Function: Find the absolute value of the data
Format: Math.abs (value);
Return value: the absolute value of the data;

        console.log(Math.abs(-1));

Insert picture description here

8), pow method

Function: Find x to the power of y
Format: Math.pow(x,y);

  console.log(Math.pow(2, 10));

Insert picture description here

9), sqrt method:
Function: Find the square root of the data
Format: Math.sqrt (data);

        console.log(Math.sqrt(4));

Insert picture description here
Fourth, toFixed method
Function: Process the decimal places of a data
Format: data.toFixed (decimal places);
Note: The return value of this method is a string type

        var num = Math.PI;
        var res = num.toFixed(3);
        console.log(res);
        console.log(typeof res);

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46374969/article/details/115268437