美文网首页
math.对象

math.对象

作者: 码农也会修真 | 来源:发表于2019-07-31 21:42 被阅读0次
console.log(Math.max(1, 2, 3, 4, 5, 6)); // 取最大值
console.log(Math.min(1, 2, 3, 4, 5, 6)); // 取最小值
console.log(Math.min(1, 2, 3, 'a', 5, 6)); // 有字符串所以打不出来
console.log(Math.ceil(10.1)); // 向上取整天花板
console.log(Math.floor(10.9)); // 向下取整地板
console.log(Math.round(10.5)) // 四舍五入
console.log(Math.round(10.4)) // 四舍五入

console.log(Math.floor(Math.random() * 10)); // 随机数随的数只能是10以下的数

// 随机数
Math.random()
    // 默认范围: 0 < 范围 < 1
Math.floor(Math.random() * 结束值 + 起始值);
起始值 <= 范围 <= 结束值(某个字符串的长度 - 1)

console.log(Math.abs(10)); // 绝对值

console.log(Math.pow(2, 4)); // pow:2的4次方

console.log(Math.sqrt(16)) // 16的开方


var str = 'abcdefgh';

console.log(str[Math.floor(Math.random() * 8)]);
setInterval('console.log(str[Math.floor(Math.random()*8)]);', 1000) // 随机每隔1秒打`印一下字符串str

相关文章

网友评论

      本文标题:math.对象

      本文链接:https://www.haomeiwen.com/subject/xcmxdctx.html