js笔记让前端飞程序员

js笔记十二之Math数学函数

2018-05-07  本文已影响154人  uplyw

Math中的常用方法

数学函数: 虽然它叫函数,但是它是对象数据类型的typeof Math -> object

Math对象中给我们提供了很多常用的操作数字的方法
console.dir(Math) 查看所有方法

Math.abs(12)    // -> 12
Math.abs(-12)   // -> 12
Math.ceil(12)       // -> 12
Math.ceil(12.1)     // -> 13
Math.ceil(12.9)     // -> 13
Math.ceil(-12.1)    // -> -12
Math.ceil(-12.9)    // -> -12
Math.floor(12)       // -> 12
Math.floor(12.1)     // -> 12
Math.floor(12.9)     // -> 12
Math.floor(-12.1)    // -> -13
Math.floor(-12.9)    // -> -13
 Math.round(12.3)   // -> 12
 Math.round(12.5)   // -> 13
 
 Math.round(-12.3)  // -> -12
 Math.round(-12.5)  // -> -12
 Math.round(-12.51) // -> -13
for(var i=0;i<100;i++){
    console.log(Math.random()) // 随机输出100个0-1之间的小数
}
// -> 获取[0,10]之间的随机整数
Math.round(Math.random() * 10)

// -> 获取[3,15]之间的随机整数
Math.round(Math.random()*12+3)

// -> 获取[n,m]之间的随机整数
Math.round(Math.random()*(m-n) + n)

Math.max(12,2,34,1,4,6) // -> 34
Math.min(12,2,34,1,4,6) // -> 1
Math.PI     // -> 3.141592653589793
Math.pow(10,3) // -> 1000
Math.sqrt(100)
上一篇下一篇

猜你喜欢

热点阅读