js内置对象---Math
圆周率-Math.PI
console.log(Math.PI)
求最大值 - Math.max(多个数字)
console.log( Math.max(1,5,9,7,3,4,6,2,8) );
求最小值 - Math.min(多个数字)
console.log( Math.min(1,5,9,7,3,4,6,2,8) );
求绝对值 - Math.abs(数字)
console.log( Math.abs(-3) );
向下取整 - Math.floor(数字) - 获取比这个小数小的第一个整数
console.log( Math.floor(3.98) );
向上取整 - Math.ceil(数字) - 获取比这个小数大的第一个整数
console.log( Math.ceil(3.0000005) );
四舍五入取整 - Math.round(数字)
console.log( Math.round(5.6) );
console.log( Math.round(5.16) );
求次方 - Math.pow(底数, 幂)
console.log( Math.pow(2, 3) ); 2的3次方
开平方根 - Math.sqrt(数字)
console.log( Math.sqrt(4) );
求正弦值 - Math.sin(弧度) - 弧度 = 角度 * π / 180
console.log( Math.sin(30 * Math.PI / 180) );
求余弦值 - Math.cos(弧度)
console.log( Math.cos(60 * Math.PI / 180) );
求随机数 - Math.random()
console.log( Math.random() ); 包0不包1的小数
需要包0不包10的随机整数
console.log( Math.floor(Math.random() * 10) );
包5不包15的整数
console.log( Math.floor(Math.random() * 10) + 5 );
求包20不包100的整数
console.log( Math.floor(Math.random() * 80) + 20 );
本文介绍了JavaScript中Math对象的一些基本功能,包括获取圆周率Math.PI,求最大值Math.max,最小值Math.min,绝对值Math.abs,向下取整Math.floor,向上取整Math.ceil,四舍五入取整Math.round,次方运算Math.pow,开平方根Math.sqrt,以及三角函数Math.sin和Math.cos的使用。此外,还展示了如何生成随机数及其应用示例。
1691

被折叠的 条评论
为什么被折叠?



