日期:2014-05-16  浏览次数:20353 次

js Math中一些常用的方法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled Document</title>
</head>
<script>
function test(){
var a = Math.abs(-23);
alert("-23的绝对值:"+a);

var b = Math.ceil(52.25);//向上取整
alert("返回的值大于等于52.25,并等于某个整数:"+b);

var floor = Math.floor(52.55);//向下取整
alert("向下取整:"+floor);

var max = Math.max(23,52);
alert("返回两者(23和52)中的较大者:"+max);

var random = Math.random();
alert("产生随机数(0~1):"+random);

var exp = Math.exp(2);
alert("返回欧拉数e的2(double类型)次幂的值:"+exp);

var url = "http://www.baidu.com/"+(10*Math.random());
alert(url);

var log = Math.log(10);
alert("返回10(double类型)的自然对数"+log);

var sqrt = Math.sqrt(3);
alert("返回3的平方根:"+sqrt);

var round1 = Math.round(23.26);//四舍五入
var round2 = Math.round(23.66);//四舍五入
alert("四舍五入(23.26和23.66):"+round1+"和"+round2);
}
</script>
<body>
<a href="javascript:test()">测试</a>
</body>
</html>