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="计算面积" onclick="document.form1.area.value=document.form1.rad.value * document.form1.rad.value* Math.PI" />
    <input type="button" name="btn2" value="计算平方根" onclick="document.form1.area.value=Math.sqrt(document.form1.rad.value)" />
    <input type="button" name="btn3" value="数字舍入" onclick="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>



你可能感兴趣的:(Javascript学习笔录12(Math用法))