Math数学操作类方法

本文介绍了JavaScript中Math对象的一些基本功能,包括获取圆周率Math.PI,求最大值Math.max,最小值Math.min,绝对值Math.abs,向下取整Math.floor,向上取整Math.ceil,四舍五入取整Math.round,次方运算Math.pow,开平方根Math.sqrt,以及三角函数Math.sin和Math.cos的使用。此外,还展示了如何生成随机数及其应用示例。

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 );

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值