JavaScript Math方法的基本使用

1.Math.sin()方法

定义:返回一个数的正弦。

语法:Math.sin(x),x必须是一个数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回1的正弦值</p>
<button onclick="sin()">点我</button>
<script>
function sin(){
	document.getElementById("demo").innerHTML=Math.sin(1);
}
</script>
</body>
</html>

2.Math.cos()方法

定义:返回一个数的余弦。

语法:Math.cos(x),x必须是一个数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回1的余弦值</p>
<button onclick="cos()">点我</button>
<script>
function cos(){
	document.getElementById("demo").innerHTML=Math.cos(1);
}
</script>
</body>
</html>

3.Math.tan()方法

定义:返回一个表示某个角的正切的数字。

语法:Math.tan(x); x必须是一个数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回1的正切值</p>
<button onclick="tan()">点我</button>
<script>
function tan(){
	document.getElementById("demo").innerHTML=Math.tan(1);
}
</script>
</body>
</html>

 

4.Math.asin()方法

定义:返回一个数的反正弦值。

语法:Math.asin(x),x必须是-1.0~1.0之间的数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回1的反正弦值</p>
<button onclick="asin()">点我</button>
<script>
function asin(){
	document.getElementById("demo").innerHTML=Math.asin(1);
}
</script>
</body>
</html>

5.Math.acos()方法

定义:返回一个数的反余弦值。

语法:Math.acos(x);x必须是-1.0~1.0之间的数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回1的反余弦值</p>
<button onclick="acos()">点我</button>
<script>
function acos(){
	document.getElementById("demo").innerHTML=Math.acos(1);
}
</script>
</body>
</html>

6.Math.atan()方法

定义:返回一个表示某个角的反正切的数字。

语法:Math.atan(x);x必须是一个数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回1的反正切值</p>
<button onclick="atan()">点我</button>
<script>
function atan(){
	document.getElementById("demo").innerHTML=Math.atan(1);
}
</script>
</body>
</html>

 

7.Math.abs()方法

定义:返回传入值的绝对值。

语法:Math.abs(x);x必须是一个数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回-11.21的绝对值</p>
<button onclick="abs()">点我</button>
<script>
function abs(){
	document.getElementById("demo").innerHTML=Math.abs(-11.21);//返回11.21
}
</script>
</body>
</html>

 

8.Math.ceil()方法

定义:对一个数进行向上舍入(进一法)。

语法:Math.ceil(x);x必须是一个数值,如果传入值是整数,则该值不变。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回Math.ceil(1.1)的值</p>
<button onclick="ceil()">点我</button>
<script>
function ceil(){
	document.getElementById("demo").innerHTML=Math.ceil(1.1);  //返回2
}
</script>
</body>
</html>

9.Math.floor()方法

定义:返回一个小等于x的最大整数(向下取整)。

语法:Math.floor(x);x必须是一个数值,如果传入值是整数,则该值不变。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回Math.floor(1.1)的值</p>
<button onclick="floor()">点我</button>
<script>
function floor(){
	document.getElementById("demo").innerHTML=Math.floor(1.1);  //返回1
}
</script>
</body>
</html>

10.Math.max()方法

定义:返回传入值中的最大值。

语法:Math.max(n1,n2,n3,...,nx);n必须是数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回传入值(1 ,1.1 ,5)中的最大值</p>
<button onclick="max()">点我</button>
<script>
function max(){
	document.getElementById("demo").innerHTML=Math.max(1,1.1,5);//返回5
}
</script>
</body>
</html>

 

11.Math.min()方法

定义:返回传入值中的最小值

语法:Math.min(n1,n2,n3,...,nx);n必须是数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回传入值(1,1.1,5)中的最小值</p>
<button onclick="min()">点我</button>
<script>
function min(){
	document.getElementById("demo").innerHTML=Math.min(1,1.1,5);//返回1
}
</script>
</body>
</html>

 

12.Math.pow()方法

定义:返回一个数的n次幂。

语法:Math.pow(x,y);x,y必须是数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回2的3次方</p>
<button onclick="pow()">点我</button>
<script>
function pow(){
	document.getElementById("demo").innerHTML=Math.pow(2,3); //返回8
}
</script>
</body>
</html>

 

13.Math.random()方法

定义:返回一个介于 0(包含) ~ 1(不包含) 之间的随机数:

语法:Math.random();

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回一个随机数</p>
<button onclick="ceil()">点我</button>
<script>
function ceil(){
	document.getElementById("demo").innerHTML=Math.random();//返回一个随机数
}
</script>
</body>
</html>

14.Math.round()方法

定义:将传入值进行四舍五入

语法:Math.round(x);x必须是一个数值

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回2.1四舍五入后的值</p>
<button onclick="round()">点我</button>
<script>
function round(){
	document.getElementById("demo").innerHTML=Math.round(2.1); //返回2
}
</script>
</body>
</html>

15.Math.sqrt()方法

定义:返回一个数的平方根

语法:Math.sqrt(x);x必须是一个数值

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回4的平方根</p>
<button onclick="sqrt()">点我</button>
<script>
function sqrt(){
	document.getElementById("demo").innerHTML=Math.sqrt(4);//返回2
}
</script>
</body>
</html>

 

16.Math.exp()方法

定义:返回E的n次幂(E为自然底数,E≈2.7183)。

语法:Math.exp(x);x必须是数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回E的二次幂</p>
<button onclick="exp()">点我</button>
<script>
function exp(){
	document.getElementById("demo").innerHTML=Math.exp(2);//返回7.38905609893065
}
</script>
</body>
</html>

17.Math.log()方法

定义:返回传入值的自然对数。

语法:Math.log(x);x必须是一个数值。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math</title>
</head>
<body>
    <p id="demo">点击返回2的自然对数</p>
<button onclick="log()">点我</button>
<script>
function log(){
	document.getElementById("demo").innerHTML=Math.log(2);//返回0.6931471805599453
}
</script>
</body>
</html>

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值