Number 对象
范围 –253 to 253
alert(Number.MIN_VALUE+"~"+Number.MAX_VALUE);
Math 对象
类似于Java里的Math类,没有构造方法,不能实例化,提供了一系列有用的静态属性和方法。
属性:
E The number value for e, the base of natural logarithms
LN2 Natural logarithm of 2
LN10 Natural logarithm of 10
LOG2E Base 2 logarithm of e, and the reciprocal of LN2
LOG10E Base 10 logarithm of e, and the reciprocal of LN10
PI The number for π
SQRT1_2 Square root of 1/2, reciprocal of SQRT2
SQRT2 Square root of 2
LN2 Natural logarithm of 2
LN10 Natural logarithm of 10
LOG2E Base 2 logarithm of e, and the reciprocal of LN2
LOG10E Base 10 logarithm of e, and the reciprocal of LN10
PI The number for π
SQRT1_2 Square root of 1/2, reciprocal of SQRT2
SQRT2 Square root of 2
方法:
abs (x) Returns absolute value of x; if x is NaN, returns NaN
acos (x) Returns arc cosine of x; if x is greater than 1 or less than 0, returns NaN
asin (x) Returns arc sine of x; if x is greater than 1 or less than –1, returns NaN
atan (x) Returns the arc tangent of x
atan2 (x, y) Returns the arc tangent of the quotient of x, y
ceil (x) Returns the smallest integer equal to or greater than x
cos (x) Returns the cosine of x
exp (x) Returns Ex where E is the base of natural logarithms
floor (x) Returns the largest integer equal to or less than x
log (x) Returns logarithm of x
max (x1, x2, ..., xn) Returns largest of given arguments
min (x1, x2, ..., xn) Returns smallest of given arguments
pow (x,y) Returns result of raising x to power of y
random () Returns random number greater than or equal to 0, and less than 1
round (x) Rounds number to closest integer
sin (x) Returns the sine of x
sqrt (x) Returns the square root of x
acos (x) Returns arc cosine of x; if x is greater than 1 or less than 0, returns NaN
asin (x) Returns arc sine of x; if x is greater than 1 or less than –1, returns NaN
atan (x) Returns the arc tangent of x
atan2 (x, y) Returns the arc tangent of the quotient of x, y
ceil (x) Returns the smallest integer equal to or greater than x
cos (x) Returns the cosine of x
exp (x) Returns Ex where E is the base of natural logarithms
floor (x) Returns the largest integer equal to or less than x
log (x) Returns logarithm of x
max (x1, x2, ..., xn) Returns largest of given arguments
min (x1, x2, ..., xn) Returns smallest of given arguments
pow (x,y) Returns result of raising x to power of y
random () Returns random number greater than or equal to 0, and less than 1
round (x) Rounds number to closest integer
sin (x) Returns the sine of x
sqrt (x) Returns the square root of x
进制转换
运用 Number 对象的 toString 方法,参数范围 2~36
alert("8 octal :"+decNum.toString(8)+"; 16 hexadecimal:"+decNum.toString(16)+"; 2 binary:"+decNum.toString(2));
随机数
Math 的 random 方法 ,产生一个0到1 之间的随机数。
示例:随机颜色
function randomVal(val) {
return Math.floor(Math.random() * val);
}
function randomColor() {
return "rgb(" + randomVal(255) + "," + randomVal(255) + "," + randomVal(255) + ")";
}
return Math.floor(Math.random() * val);
}
function randomColor() {
return "rgb(" + randomVal(255) + "," + randomVal(255) + "," + randomVal(255) + ")";
}
或者用16进制的形式
function randomColor() {
// get red
var r = randomVal(255).toString(16);
if (r.length < 2) r= "0" + r;
// get green
var g = randomVal(255).toString(16);
if (g.length < 2) g= "0" + g;
// get blue
var b = randomVal(255).toString(16);
if (b.length < 2) b= "0" + b;
return "#" + r + g + b;
}
// get red
var r = randomVal(255).toString(16);
if (r.length < 2) r= "0" + r;
// get green
var g = randomVal(255).toString(16);
if (g.length < 2) g= "0" + g;
// get blue
var b = randomVal(255).toString(16);
if (b.length < 2) b= "0" + b;
return "#" + r + g + b;
}
转载于:https://blog.51cto.com/lovespss/413480