JavaScript学习笔记—Math

  • 工具类
  • 为我们提供了数学运算相关的一些常量和方法
  • 常量
    (1)Math.PI 圆周率
  • 方法
    (1)Math.abs() 求一个数的绝对值
    (2)Math.min() 求多个值中的最小值
    (3)Math.max() 求多个值中的最大值
    (4)Math.pow() 求x的y次幂
    (5)Math.sqrt() 求一个数的平方根
    (6)Math.floor() 向下取整
    (7)Math.ceil() 向上取整
    (8)Math.round() 四舍五入取整
    (9)Math.trunc() 直接去除小数位
    (10)Math.random() 返回一个0到1之间(不包括1)的随机数
let result = Math.abs(-10);
result = Math.min(10, 20, 30, 44, 55, -1);
result = Math.max(10, 20, 30, 44, 55, -1);
result = Math.pow(2, 3); // 等价于 2 ** 3
result = Math.sqrt(4); // 等价于 4 ** .5
result = Math.floor(1.2); // 1
result = Math.floor(1.9); // 1
result = Math.floor(-1.2); // -2
result = Math.ceil(1.2); // 2
result = Math.ceil(-1.8); // -1
result = Math.round(1.5); // 2
result = Math.round(-1.5); // -1
result = Math.trunc(1.5); // 1
result = Math.trunc(-1.5); // -1
result = Math.trunc(-1.5); // -1
// 得到一个两数之间的随机整数
result = Math.round(Math.radom() * (max - min) + min)
posted @ 2023-01-26 13:27  程序员张3  阅读(16)  评论(0编辑  收藏  举报