JavaScript Math 对象属性与方法

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 对象方法
  • abs(a):返回绝对值
//返回绝对值
<script type = “text/javascript”>
    alert(Math.abs(-6)); //运行结果为 6
</script>
  • ceil(a):向上取整
//向上取整
<script type = “text/javascript”>
    alert(Math.ceil(3.1)); //运行结果为 4
</script>
  • floor(a):向下取整
//向下取整
<script type = “text/javascript”>
    alert(Math.floor(3.9)); //运行结果为 3
</script>
  • round(a):四舍五入
//四舍五入
<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>
  • max(a,b):返回a、b较大的数
//返回a、b较大的数
<script type = “text/javascript”>
    alert(Math.max(5,8)); //运行结果为 8
</script>
  • pow(a,n):a的n次幂
//a的n次幂
<script type = “text/javascript”>
    alert(Math.pow(2,5)); //运行结果为 32
</script>
  • random():生成随机数,区间[0,1]
//生成随机数,区间[0,1]
<script type = “text/javascript”>
    alert(Math.random( ));
</script>
  • sqrt(a):平方根
//平方根
<script type = “text/javascript”>
    alert(Math.sqrt( 4)); //运行结果为 2
</script>
  • sin(弧度):正弦值
//求90度的正弦值
<script type = “text/javascript”>
    alert(Math.sin(90* Math.PI/180)); //运行结果为 1
</script>
  • cos(弧度):余弦值
//余弦值
<script type = “text/javascript”>
    alert(Math.cos(180* Math.PI/180)); //运行结果为 1
</script>
  • tan(弧度):正切值
  • asin(a):反正弦值
  • acos(a):反余弦值
  • atan(弧度):反正切值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值