JavaScript Math方法的基本使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JavaScript Math方法的基本使用

fjxylin   2020-02-28 我要评论

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 οnclick="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 οnclick="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 οnclick="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 οnclick="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 οnclick="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 οnclick="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 οnclick="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 οnclick="ceil()">点我</button>
<script>
function ceil(){
    document.getElementById("demo").innerHTML=Math.ceil(1.1);  //返回2
}
</script>
</body>
</html>

8.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 οnclick="floor()">点我</button>
<script>
function floor(){
    document.getElementById("demo").innerHTML=Math.floor(1.1);  //返回1
}
</script>
</body>
</html>

9.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 οnclick="max()">点我</button>
<script>
function max(){
    document.getElementById("demo").innerHTML=Math.max(1,1.1,5);//返回5
}
</script>
</body>
</html>

10.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 οnclick="min()">点我</button>
<script>
function min(){
    document.getElementById("demo").innerHTML=Math.min(1,1.1,5);//返回1
}
</script>
</body>
</html>

11.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 οnclick="pow()">点我</button>
<script>
function pow(){
    document.getElementById("demo").innerHTML=Math.pow(2,3); //返回8
}
</script>
</body>
</html>

12.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 οnclick="ceil()">点我</button>
<script>
function ceil(){
    document.getElementById("demo").innerHTML=Math.random();//返回一个随机数
}
</script>
</body>
</html>

13.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 οnclick="round()">点我</button>
<script>
function round(){
    document.getElementById("demo").innerHTML=Math.round(2.1); //返回2
}
</script>
</body>
</html>

14.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 οnclick="sqrt()">点我</button>
<script>
function sqrt(){
    document.getElementById("demo").innerHTML=Math.sqrt(4);//返回2
}
</script>
</body>
</html>

15.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 οnclick="exp()">点我</button>
<script>
function exp(){
    document.getElementById("demo").innerHTML=Math.exp(2);//返回7.38905609893065
}
</script>
</body>
</html>

16.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 οnclick="log()">点我</button>
<script>
function log(){
    document.getElementById("demo").innerHTML=Math.log(2);//返回0.6931471805599453
}
</script>
</body>
</html>

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们