04.Math.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script>
  9. /*
  10. Math
  11. -Math和其他的对象不同,它不是一个构造函数,
  12. 它属于一个工具类,不用创建对象,它里面封装了数学运算相关的属性和方法
  13. -比如
  14. Math.PI 表示圆周率
  15. */
  16. // console.log(Math.PI);
  17. /*
  18. abs()可以用来计算一个数的绝对值
  19. */
  20. // console.log(Math.abs(5));
  21. // console.log(Math.abs(-5));
  22. /*
  23. Math.ceil()
  24. 可以对一个数进行向上取整,小数位只要有值就自动进1
  25. Math.floor()
  26. 可以对一个数进行向下取整,小数部分会被舍掉
  27. Math.round()
  28. 可以对一个数进行四舍五入取整
  29. */
  30. // console.log(Math.ceil(1.4));
  31. // console.log(Math.floor(1.99));
  32. // console.log(Math.round(1.4));
  33. /*
  34. Math.random()
  35. -可以用来生成一个0-1之间的随机数
  36. -生成一个0-10的随机数
  37. 生成一个0-x之间的随机数
  38. Math.round(Math.random()*x))
  39. */
  40. // console.log(Math.random());
  41. // for(var i = 0 ; i < 5 ; i++){
  42. // // console.log(Math.random()*10);
  43. // // console.log(Math.round(Math.random()*10));
  44. // }
  45. /*
  46. 生成1-10
  47. 生成一个x-y之间的随机数
  48. Math.round(Math.random()*(y-x))+x
  49. */
  50. // for(var i = 0 ; i < 5 ; i++){
  51. // console.log(Math.round(Math.random()*9)+1);
  52. // }
  53. /*
  54. max()可以获取多个数中的最大值
  55. min()可以获取多个数中的最小值
  56. */
  57. // var max = Math.max(10,45,30);
  58. // var min = Math.min(10,45,30);
  59. // console.log(max);
  60. // console.log(min);
  61. /*
  62. Math.pow(x,y)
  63. 返回x的y次幂
  64. */
  65. // console.log(Math.pow(2,3));//输出结果为8
  66. /*
  67. Math.sqrt()
  68. 用于对一个数进行开方运算
  69. */
  70. console.log(Math.sqrt(9));//输出结果为3
  71. </script>
  72. </head>
  73. <body>
  74. </body>
  75. </html>