前端Math属性方法汇总集锦

Description of Math

Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。

Math 用于 Number 类型。它不支持 BigInt

与其他全局对象不同的是,Math 不是一个构造器。Math 的所有属性与方法都是静态的。引用圆周率的写法是 Math.PI,调用正余弦函数的写法是 Math.sin(x)x 是要传入的参数。Math 的常量是使用 JavaScript 中的全精度浮点数来定义的。

1. Math.ceil 向上取整

const a = '4.4'
    console.log('Math.ceil', Math.ceil(a)) // 5

 2. Math.floor 向下取整

const a = '4.4'
    console.log('Math.floor', Math.floor(a)) // 4

3.Math.round 四舍五入

const a = '4.6'
    console.log('Math.round', Math.round(a)) // 5

4. Math.random 0-1 随机数

console.log('Math.random', Math.random())

5.Math.max  最大值 

前端数组方法汇总集锦-CSDN博客

6.Math.min  最小值 

前端数组方法汇总集锦-CSDN博客

7.Math.abs 绝对值

const a = '-4.6'
    console.log('Math.abs', Math.abs(a)) // 4.6

8. Math.sqrt 开平方

const a = '9'
    console.log('Math.sprt', Math.sqrt(a)) // 3

9. Math.pow 求幂

const a = '3'
    console.log('Math.pow', Math.pow(a,2)) // 9

10. Math.trunc 整数部分

const a = '4.5'
    console.log('整数部分', Math.trunc(a)) // 4

11.Math.PI

function calculateCircumference(radius) {
  return 2 * Math.PI * radius;
}

calculateCircumference(1); // 6.283185307179586

12.Math.clz32()

console.log('获取相应的内容', Math.clz32(1000000))  // 12

13.Math.sign()   判断是否为正数/负数

console.log(Math.sign(3)); //  1
    console.log(Math.sign(-3)); // -1
    console.log(Math.sign("-3")); // -1
    console.log(Math.sign(0)); //  0
    console.log(Math.sign(-0)); // -0
    console.log(Math.sign(NaN)); // NaN
    console.log(Math.sign("foo")); // NaN
    console.log(Math.sign()); // 

猜你喜欢

转载自blog.csdn.net/2201_75705263/article/details/134560676