JavaScript學習:JavaScript數學 Math對象

JavaScript Math 對象允許您對數字執行數學任務。
一、Math.PI;//返回3.141592653589793
**二、Math.round()**返回值是四捨五入最接近的整數。
例:
Math.round(6.8); // 返回 7
Math.round(2.3); // 返回 2
三、Math.pow() Math.pow(x, y) 的返回值是 x 的 y 次冪
例:
Math.pow(8, 2); // 返回 64
四、Math.sqrt() Math.sqrt(x) 返回 x 的平方根
例:
Math.sqrt(64); // 返回 8
五、Math.abs() Math.abs(x) 返回 x 的絕對(正)值
例:
Math.abs(-4.7); // 返回 4.7
六、Math.ceil() Math.ceil(x) 的返回值是 x 上舍入最接近的整數
例:
Math.ceil(6.4); // 返回 7
七、Math.floor() Math.floor(x) 的返回值是 x 下舍入最接近的整數
例:
Math.floor(2.7); // 返回 2
八、Math.sin() Math.sin(x) 返回角 x(以弧度計)的正弦(介於 -1 與 1 之間的值)
也可以將角度轉換爲弧度:Angle in radians = Angle in degrees x PI / 180.

Math.sin(90 * Math.PI / 180); // 返回 1(90 度的正弦)
九、Math.cos() Math.cos(x) 返回角 x(以弧度計)的餘弦(介於 -1 與 1 之間的值)。
例:
Math.cos(0 * Math.PI / 180); // 返回 1(0 度的餘弦)
十、Math.min() 和 Math.max() 可用於查找參數列表中的最低或最高值。
例:
Math.min(0, 450, 35, 10, -8, -300, -78); // 返回 -300
例:
Math.max(0, 450, 35, 10, -8, -300, -78); // 返回 450
W3School JavaScript

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章