Attributes of the Math object
E: value e, the base of the natural logarithm
LN10: the natural logarithm of 10
LN2: the natural logarithm of 2
LOG2E: 2 as the base E Logarithm
LOG10E: Logarithm of E with base 10
PI: Value pie
SQRT1_2: Square root of 1/2
SQRT2: Square root of 2
Math object method: Maximum Value and minimum value
min()&&max() is used to get the minimum and maximum value in a set of numbers.
Example:
var iMax=Math.Max(1,2,3);
alert(iMax);//outputs 3
var iMin=Math.Min(1,2,3);
alert(iMin); //outputs 1
Approximate value
abs() is used to return the absolute value of a number.
Example:
var iNegOne=Math.abs( -1);
alert(iNegOne);//oupputs 1
var iPosOne=Math.abs(1);
alert(iPosOne);//outputs 1
Round decimals into integers
ceil() is an upward rounding function, always rounding numbers up to the nearest value
floor() is a downward rounding function, always Rounds the number down to the nearest value
round() is the rounding method
Example:
alert(Math.ceil(25.5));//oputpus 26
alert(Math.floor(25.5));//oputpus 25
alert(Math.round(25.5));//oputpus 26
Exponent calculation
exp() is used to raise Math.E to the specified power
log() is used to return the natural logarithm of a specific number
pow() is used to raise the specified number to the specified power
sqrt() is used to return the square root of the specified number
Trigonometric Function method
acos(x) is used to return the arc cosine value of x
asin(x) is used to return the arc sine value of x
atan(x) is used to return the arc tangent value of x
atan2(y,x) is used to return the inverse cosine value of y/x
cos(x) is used to return the cosine value of x
sin(x) is used to return the sine value of x
tan(x) is used to return the tangent value of x
Random number function
random() is used to return a random number between 0 and 1, excluding 0 and 1 Select random numbers within a certain range:
function selectFrom(iFirstValue,iLastValue){
var iChoices=iLastValue-iFirstValue 1;
return Math.floor(Math.random()*iChoices iFirstValue);
}
//demo
var iNum=selectFrom(2,10);
Author: Artwl
Source: http://artwl.cnblogs.com