標籤:
js-math對象
一. Math 對象
Math 對象,是數學對象,提供對資料的數學計算
注意:math對象是一個固有對象,無需建立它,直接把math作為對象使用就可以調用其所有屬性和方法。這是它與date,string對象的區別
二、Math對象屬性
| 屬性 |
描述 |
| E |
返回算術常量 e,即自然對數的底數(約等於2.718281828459045)。 |
| LN2 |
返回 2 的自然對數(約等於0.693)。 |
| LN10 |
返回 10 的自然對數(約等於2.302)。 |
| LOG2E |
返回以 2 為底的 e 的對數(約等於 1.414)。 |
| LOG10E |
返回以 10 為底的 e 的對數(約等於0.434)。 |
| PI |
返回圓周率(約等於3.141592653589793)。 |
| SQRT1_2 |
返回返回 2 的平方根的倒數(約等於 0.707)。 |
| SQRT2 |
返回 2 的平方根(約等於 1.414)。 |
三、Math對象方法
| 方法 |
描述 |
| abs(x) |
返回數的絕對值。 |
| acos(x) |
返回數的反餘弦值。 |
| asin(x) |
返回數的反正弦值。 |
| atan(x) |
以介於 -PI/2 與 PI/2 弧度之間的數值來返回 x 的反正切值。 |
| atan2(y,x) |
返回從 x 軸到點 (x,y) 的角度(介於 -PI/2 與 PI/2 弧度之間)。 |
| ceil(x) |
對數進行上舍入。 |
| cos(x) |
返回數的餘弦。 |
| exp(x) |
返回 e 的指數。 |
| floor(x) |
對數進行下舍入。 |
| log(x) |
返回數的自然對數(底為e)。 |
| max(x,y) |
返回 x 和 y 中的最高值。 |
| min(x,y) |
返回 x 和 y 中的最低值。 |
| pow(x,y) |
返回 x 的 y 次冪。 |
| random() |
返回 0 ~ 1 之間的隨機數。 |
| round(x) |
把數四捨五入為最接近的整數。 |
| sin(x) |
返回數的正弦。 |
| sqrt(x) |
返回數的平方根。 |
| tan(x) |
返回角的正切。 |
| toSource() |
返回該對象的原始碼。 |
| valueOf() |
返回 Math 對象的原始值。 |
四、執行個體
1、Math.abs(value) :返回絕對值
Math.abs(‘123‘); // => 123 :純數字字串Math.abs(‘-123‘); // => 123Math.abs(123); // => 123Math.abs(-123); // => 123Math.abs(‘123a‘); // => NaN :非純數字字串
注意:參數:value {Number | NumberStr} :數字或者純數位字串。 傳回值: {Number} 返回參數的絕對值數字。若參數不為數字,返回NaN。
2、Math.ceil(value) : 對一個數向上取整,並不是四捨五入
Math.ceil(2.7); // => 3Math.ceil(2.3); // => 3 :2.3 向上取整返回 3Math.ceil(-2.7); // => -2Math.ceil(-2.3); // => -2Math.ceil(‘2.7‘); // => 3 :純數字字串Math.ceil(‘2.7a‘); // => NaN :非純數字字串
注意:參數:value {Number | NumberStr} :數字或者純數位字串。傳回值:{Number} 返回取整後的值。若參數不為數字,返回NaN。
3、 Math.floor(value) :對一個數向下取整,並不是四捨五入
Math.floor(2.7); // => 2 Math.floor(2.3); // => 2 Math.floor(-2.7); // => -3 :-2.7 向下取整返回 -3 Math.floor(-2.3); // => -3 Math.floor(‘2.7‘); // => 2 :純數字字串 Math.floor(‘2.7a‘); // => NaN :非純數字字串
注意:參數:value {Number | NumberStr} :數字或者純數位字串。傳回值:{Number} 返回取整後的值。若參數不為數字,返回NaN。
4、 Math.max(value1,value2...valueN) :返回參數中最大的值
Math.max(1, 2, 3, 4, 5); // => 5 Math.max(1, 2, 3, 4, ‘5‘ ); // => 5 Math.max(1, 2, 3, 4, ‘a‘); // => NaN
注意:參數:value1,value2.....valueN {Number | NumberStr} :數字或者純數位字串。傳回值:{Number} 返回最大值。若一個參數不為數字,返回NaN。
5、 Math.min(value1,value2...valueN) :返回參數中最小的值
Math.min(1, 2, 3, 4, 5); // => 1 Math.min(‘1‘, 2, 3, 4, 5); // => 1 Math.min(1, 2, 3, 4, ‘a‘); // => NaN
注意:參數:value1,value2.....valueN {Number | NumberStr} :數字或者純數位字串。傳回值:{Number} 返回最大值。若一個參數不為數字,返回NaN。
6、 Math.pow(x,y) :返回x的y次方
Math.pow(2, 3); // => 8 :2的3次方 Math.pow(3, 2); // => 9 :3的2次方 Math.pow(‘4‘, 2); // => 16 :4的2次方 Math.pow(‘2a‘, 2); // => NaN
注意:參數:x,y {Number | NumberStr} :數字或者純數位字串。傳回值:{Number} 返回x的y次方。若一個參數不為數字,返回NaN。
7、Math.random() :返回一個偽隨機數,大於0,小於1.0
Math.random(); // => 0.8982374747283757 Math.random(); // => 0.39617531932890415 Math.random(); // => 0.35413061641156673 Math.random(); // => 0.054441051790490746
注意:參數:無 傳回值:{Number} 返回一個偽隨機數,大於0,小於1.0
8、Math.round(value) : 四捨五入後取整
Math.round(2.5); // => 3 Math.round(2.4); // => 2 Math.round(-2.6); // => -3 Math.round(-2.5); // => -2 :-2.5四捨五入為 -2 Math.round(-2.4); // => -2 Math.round(‘2.7‘); // => 3 :純數字字串 Math.round(‘2.7a‘); // => NaN :非純數字字串
注意:參數:value {Number | NumberStr} :數字或者純數位字串。傳回值:{Integer} 返回參數四捨五入後的整數。若參數不為數字,返回NaN。
9、Math.sqrt(value) :返回參數的平方根
console.log( Math.sqrt(9) ); // => 3 console.log( Math.sqrt(16) ); // => 4 console.log( Math.sqrt(‘25‘) ); // => 5 console.log( Math.sqrt(‘a‘) ); // => NaN
注意:參數:value {Number | NumberStr} :數字或者純數位字串傳回值:{Number} 返回參數的平方根
js-math對象