JavaScript利用Math算数

本文介绍了JavaScript中的Math对象,包括round、random、floor、max、min等方法以及cos、sin、tan等数学函数的使用,同时提到了Math对象的一些常量如E、PI等,展示了如何在实际开发中进行算数操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发工具与关键技术:visual Studio jquery
作者:邓伟征
撰写时间:

Math(算数)对象的作用是:执行普通的算数任务。
Math对象提供多种算数数值类型和函数。无需在使用这个对象之前对它进行定义。
round方法:对一个数进行四舍五入
document.write(Math.round(9.9) + “
”);
random()方法:返回一个介于0到1自己的随机数
document.write(Math. random + “
”);

Floor方法:去除余数取整数
document.write(Math.floor(8.8) + “
”);
使用了 Math 对象的 floor() 方法和 random() 来返回一个介于 0 和 100之间的随机数
document.write(Math.floor(Math.random() * 111))
在这里插入图片描述
在这里插入图片描述
Max()方法:获取数组中最大的数
document.write(Math.max(13, 12,11) + “
”)
document.write(Math.max(14, 4,6) + “
”)
document.write(Math.max(-3, 5,-1) + “
”)
document.write(Math.max(20, 7.30))
在这里插入图片描述
Min()方法:获取数组中最小的数
document.write(Math.min(5,99,88) + “
”)
document.write(Math.min(20,30,49) + “
”)
document.write(Math.min(13, 40,55) + “
”)
document.write(Math.min(14, 33.30))
在这里插入图片描述
cos方法:返回数的余弦值
document.write(Math.cos(45) + “
”)
sin(x)方法: 返回数的正弦的值
document.write(Math.sin(45) + “
”)、
tan(x)方法: 返回角的正切
document.write(Math.tan(45) + “
”)
在这里插入图片描述

exp()方法:返回 e 的指数
document.write(Math.exp(45) + “
”)
sqrt()方法:返回数的平方根
document.write(Math.sqrt(45) + “
”)
在这里插入图片描述
ads()方法:返回数的绝对值
document.write(Math.abs(45) + “
”)
atan()方法:以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
document.write(Math.atan(45) + “
”)
ayan2()方法:返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
document.write(Math.ayan2(45) + “
”)
ceil()方法:对数进行上舍入
document.write(Math.ceil(45) + “
”)
log()方法:返回数的自然数(底为e)
document.write(Math.log(45) + “
”)
pow()方法:返回x的y次幂
document.write(Math.pow(45) + “
”)
除了方法JavaScript还提供了8种可被Math对象访问的算数值:
Math.E:常数 Math.PI:圆周率 Math.SQRT2:2的平方根
Math.SQRT1_2:1/2的平方根 Math.LN2:2 的自然对数 Math.LN10:10 的自然对数
Math.LOG2E:以 2 为底的 e 的对数 Math.LOG10E:以 10 为底的 e 的对数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值