JS Math对象的使用


JS Math对象常用于数学运算

关键用法

  • Math.PI等常数
  • Math.round()等函数

代码

<!DOCTYPE html>
<html>
<head>
<title>JS Math对象</title>
<style type='text/css'>
</style>
<script type="text/javascript">
    //   =====================================================================
    // Math 对象用于执行数学任务
    Math.PI //约为3.14159
    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.SQRT1_2	//返回返回 2 的平方根的倒数(约等于 0.707)。
    Math.SQRT2     //返回 2 的平方根(约等于 1.414)
    
    alert(Math.round(3.5));//四舍五入
    alert(Math.random());// 0-1间的随机数
    alert(Math.max(10,20,30));//返回较大的数
    alert(Math.min(10,20,30));// 返回较小的数
    alert(Math.abs(-10));// 返回绝对值
    alert(Math.ceil(3.1))//向上取整
    alert(Math.floor(3.1))//向下取整
    alert(Math.pow(2,3));//2的3次方 = 8
    alert(Math.sqrt(4));//开平方
    // Math.PI = 180弧度
    // 1弧度 = Math.PI/180
    // 参数为弧度
    alert(Math.sin(Math.PI/4));
    alert(Math.cos(Math.PI/4));
    alert(Math.tan(Math.PI/4));
    //   acos(x)	返回数的反余弦值。
    // asin(x)	返回数的反正弦值。
    // atan(x)	以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
    alert(Math.asin(1));
    alert(Math.acos(1));
    alert(Math.atan(1));
</script>
</head>
<body>
</body>
</html>
发布了40 篇原创文章 · 获赞 57 · 访问量 2768

猜你喜欢

转载自blog.csdn.net/weixin_44984664/article/details/104866880