Math對象
在我們js中其實是有很多數學計算的需求的,不過不必擔心
系統給我們提供了大量的數學運算的方法供我們使用
而這些方法全都存在於我們的Math對象中
Math常用的屬性:Math.PI 相當於π 3.14159
Math對象常用的函數:
1.Math.round() 四捨五入
舉個小例子:
<script type = 「text/javascript」>
alert( Math.round( 3.4 ) );
</script>
運行結果如下
如果改為3.5的話
<script type = 「text/javascript」>
alert( Math.round( 3.5 ) );
</script>
運行結果就為4
2.Math.random() 0到1之間的隨機數
舉個小例子:
alert( Math.random( ) );
運行結果
3.
Math.max() 返回較大的數
Math.min() 返回較小的數
舉個小例子:
alert( Math.max(10,20,30) );
運行結果如下
alert( Math.min(10,20,30) );
運行結果為10,與max正好相反
4.Math.abs() 返回數的絕對值
舉個小例子:
alert( Math.abs(-10) );
運行結果
5.
Math.ceil() 向上取整
Math.floor() 向下取整
舉個小例子:
alert( Math.ceil( 3.1 ) );
運行結果:
alert( Math.floor( 3.9 ) );
運行結果
6.Math.pow( x,y); 求x的y次方
舉個小例子:
alert( Math.pow( 2,5 ) );
運行結果:
7.Math.sqrt(); 求開平方
舉個小例子:
alert( Math.sqrt( 4 ) );
運行結果:
以上是一部分的Math的數學函數,更多的這裡不一一介紹,大家可以自行搜索一下
Math的勾股函數
Math對象勾股函數
參數:都應該是弧度。Math.PI =180弧度
1弧度=Math.PI/180;
Math.sin() / cos() /tan()正弦/餘弦/正切
舉個小例子:
假設我們求30度弧度的正弦值
alert( Math.sin( 30*Math.PI/180) );
運行結果:
我們再來求一下90度的正弦值
alert(Math.sin(90* Math.PI / 180));
運行結果:
然後再來看一下cos的小例子:
alert(Math.cos(60* Math.PI/2));
運行結果:
注意:
這個計算結果有一點小誤差,這是正常現象
- 寫作不易,大家多多關注,謝謝啦 -
---web分享,分享的不只是web