Math 对象属性
-
Math.E:返回算术常量 e,即自然对数的底数(约等于2.718)
-
Math.LN2:返回 2 的自然对数(约等于0.693)
-
Math.LN10:返回 10 的自然对数(约等于2.302)
-
Math.LOG2E:返回以 2 为底的 e 的对数(约等于 1.414)
-
Math.LOG10E:返回以 10 为底的 e 的对数(约等于0.434)
-
Math.PI:返回圆周率(约等于3.14159)
-
Math.SQRT1_2:返回返回 2 的平方根的倒数(约等于 0.707)
-
Math.SQRT2:返回 2 的平方根(约等于 1.414)
Math 对象方法
//返回绝对值
<script type = “text/javascript”>
alert(Math.abs(-6)); //运行结果为 6
</script>
//向上取整
<script type = “text/javascript”>
alert(Math.ceil(3.1)); //运行结果为 4
</script>
//向下取整
<script type = “text/javascript”>
alert(Math.floor(3.9)); //运行结果为 3
</script>
//四舍五入
<script type = “text/javascript”>
alert(Math.round(3.4)); //运行结果为 3
</script>
<script type = “text/javascript”>
alert(Math.round(3.5)); //运行结果为 4
</script>
-
log(a):a的自然对数
-
min(a,b):返回a、b较小的数
//返回a、b较小的数
<script type = “text/javascript”>
alert(Math.min(5,8)); //运行结果为 5
</script>
//返回a、b较大的数
<script type = “text/javascript”>
alert(Math.max(5,8)); //运行结果为 8
</script>
//a的n次幂
<script type = “text/javascript”>
alert(Math.pow(2,5)); //运行结果为 32
</script>
//生成随机数,区间[0,1]
<script type = “text/javascript”>
alert(Math.random( ));
</script>
//平方根
<script type = “text/javascript”>
alert(Math.sqrt( 4)); //运行结果为 2
</script>
//求90度的正弦值
<script type = “text/javascript”>
alert(Math.sin(90* Math.PI/180)); //运行结果为 1
</script>
//余弦值
<script type = “text/javascript”>
alert(Math.cos(180* Math.PI/180)); //运行结果为 1
</script>
-
tan(弧度):正切值
-
asin(a):反正弦值
-
atan(弧度):反正切值
-
acos(a):反余弦值
本文章由javascript技术分享原创和收集
发表评论 (审核通过后显示评论):