2.Math对象.html 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <!--
  10. var 定义变量
  11. var a = 1;
  12. 字面量
  13. 1 2 3 4 6 true
  14. -->
  15. <script>
  16. document.write(Math.PI+'<br>'); // Math.PI => Π 常量
  17. document.write(Math.E);// 常量 固定值
  18. console.log(Math.ceil(2.167)); // 向上取整
  19. console.log(Math.floor(93.1)); // 向下取整
  20. console.log(Math.abs(-90)); // 绝对值
  21. console.log(Math.round(2.1)); //四舍五入
  22. console.log(Math.random()*10); // 随机数
  23. // 固定范围内随机数 Math.random()*(y-x)+x
  24. console.log(Math.random()*(4)+1)
  25. // 固定范围内随机数 Math.round(Math.random()*(y-x)+x)
  26. console.log(Math.round(Math.random()*(9)+11));
  27. console.log(Math.pow(3,6));//获取x的y次幂
  28. console.log(Math.sqrt(81)); //获取x的算术平方根
  29. </script>
  30. </body>
  31. </html>