<>JavaScript study Lesson Two ( two )
one ,Math object :
This object is used to handle mathematical operators , The object does not need to be created manually , Because it is window Object , When the page is loaded , The background is created automatically Math object , It can be used window.Math Ali use . Or omit window object , Direct use Math.
two ,Math Common properties of —PI
1, effect : Get pi
2, format :Math.PI
three ,Math The common methods to solve this problem
1),round method :
effect : Round the data
format : Math.round( data );
Return value : Returns the integer part of the data , That is to say, when rounding the data, the method is to process one digit after the decimal point .
console.log(Math.round(3.14));
2),ceil method :
effect : Round up the data , Take the smallest integer
format :Math.ceil( data );
Return value : The smallest integer larger than data
console.log(Math.ceil(3.74));
3)floor method :
effect : Round down the data , Take the largest integer
format :Math.floor( data );
console.log(Math.floor(3.74));
4),random method :
effect : generate 0-1 Random number of , contain 0, But not included 1
format :Math.random();
be careful :
1): If you want to generate n-m, And include n and m Random integer of :
parseInt(Math.random()*( Maximum +1- minimum value )+ minimum value );
2): If you want to generate random subscripts of arrays or strings :
parseInt(Math.random() * array .length); console.log(Math.random());
5) ,max method :
effect ; Finding the maximum value of a set of data
format :Math.max( value 1, value 2, value 3…);
6),min method :
effect : Find the minimum value in a set 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));
7),abs method :
effect : Finding the absolute value of data
format :Math.abs( value );
Return value : Absolute value of data ;
console.log(Math.abs(-1));
8),pow method
effect : seek x Of y Power
format :Math.pow(x,y);
console.log(Math.pow(2, 10));
9),sqrt method :
effect : Finding the arithmetic square root of data
format :Math.sqrt( data );
console.log(Math.sqrt(4));
four ,toFixed method
effect : To process the number of decimal places of a data
format : data .toFixed( Number of decimal places );
be careful : 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);
Technology
Daily Recommendation