nciaer 发表于 2024-8-27 13:55:05

javascript里的Math对象

Math无需实例化,都是类似静态的方法。

Math.PI; // 返回圆周率3.141592653589793
Math.abs(-10); // 返回参数的绝对值

Math.ceil(4.1); // 向上取整,返回5

Math.floor(4.8); // 向下取整,返回4

Math.round(4.5); // 四舍五入,返回5

Math.random(); // 返回0-1之间的随机数,[0, 1),如:0.43559456429386056

如果返回1-10之间的随机数呢?
Math.ceil(Math.random() * 10)

如果返回0-10之间的随机数呢?
Math.floor(Math.random() * 11)

参考链接:https://www.runoob.com/jsref/jsref-obj-math.html





页: [1]
查看完整版本: javascript里的Math对象