Javascript學習筆錄12(Math用法)

1 計算圓的面積

Math.PI

2 隨機數

Math.random():0 ~ 0.9999999(無窮小數)

Math.floor(x):返回值爲小於等於其數值參數的最大整數值,

3 平方根

Math.sqrt(x)

4 數字四捨五入

Math.round(x):返回數字最接近的整數,四捨五入,將小數值舍入到最接近的整數。

具體代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Js10.aspx.cs" Inherits="Javascript_Js10" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Javascript學習筆錄12(Math用法)</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    園的半徑:<input type="text" name="rad" /><br />
    園的面積:<input type="text" name="area" /><br />
    <input type="button" name="btn1" value="計算面積" οnclick="document.form1.area.value=document.form1.rad.value * document.form1.rad.value* Math.PI" />
    <input type="button" name="btn2" value="計算平方根" οnclick="document.form1.area.value=Math.sqrt(document.form1.rad.value)" />
    <input type="button" name="btn3" value="數字舍入" οnclick="document.form1.area.value=Math.round(document.form1.rad.value)" />
    </div>
    </form>
</body>
</html>
<script >
arr=new Array(
"這是第1句",
"這是第2句",
"這是第3句",
"這是第4句",
"這是第5句",
"這是第6句"
)

randomNO=Math.floor(arr.length * Math.random());//floor:返回值爲小於等於其數值參數的最大整數值,Math.random():0 ~ 0.9999999(無窮小數)
document.write("隨機數隨機輸出的是:"+arr[randomNO]+"</br>")
</script>



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