JavaScript 21 Math

Math是JavaScript的工具对象,用于常见的数学运算

 

 

步骤 1 : 

自然对数和圆周率

 

属性E PI,分别表示自然对数和圆周率PI

<script>
 
document.write(Math.E);
document.write("<br>");
document.write(Math.PI);
</script>

 

 

 步骤 2 : 

绝对值

<script>
document.write(Math.abs(-1));
</script>

 

步骤 3 : 

最小最大

<script>
document.write(Math.min(1,100));
document.write("<br>");
document.write(Math.max(1,100));
</script>

步骤 4 : 

求幂

方法 pow 求一个数的n次方

<script>
document.write(Math.pow(3,3)); //3的立方,即27
</script>

 

步骤 5 : 

四舍五入

<script>
document.write(Math.round(3.4));
document.write("<br>");
document.write(Math.round(3.5));
</script>

步骤 6 : 

随机数

方法 random 取0-1之间的随机数

<script>
document.write("一个 0-1 之间的随机数 : Math.random():");
document.write("<br>");
document.write(Math.random());
document.write("<br>");
document.write("十个 5-10 之间的随机数 : Math.round(Math.random() *5)+5 ");
document.write("<br>");
for(i=0;i<10;i++){
document.write(Math.round(Math.random() *5)+5 ); //5-10之间的随机整数
document.write("<br>");
}
</script>

 

 


posted @ 2020-07-24 08:01  Jasper2003  阅读(107)  评论(0编辑  收藏  举报