8.Math对象.html 992 B

123456789101112131415161718192021222324252627282930313233
  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. <script>
  10. // Math 对象
  11. document.write(Math.PI + '<br>')//常量
  12. document.write(Math.E)//常量
  13. var a = 4.6;
  14. // Math.ceil 向上取整
  15. console.log(Math.ceil(a))
  16. // Math.floor 向下取整
  17. console.log(Math.floor(a))
  18. // Math.abs 绝对值
  19. console.log(Math.abs(a))
  20. // Math.round 四舍五入
  21. console.log(Math.round(a))
  22. // Math.random 随机数
  23. console.log(Math.random())
  24. // Math.round(Math.random() * (y-x) + x)
  25. // 四舍五入 随机获取 3-10的整数
  26. console.log(Math.round(Math.random() * (10-3) + 3));
  27. console.log(Math.sqrt(81)); // Math.sqrt 算数平方根
  28. console.log(Math.pow(2,3));//Math.pow x的y次幂
  29. </script>
  30. </body>
  31. </html>