JavaScript Math数学对象、random随机数参数、ceil向上取整、floor向下取整、round四舍五入、取1到100之间的整数

Math:数学对象

Math 对象不是构造函数,它具有数学常数和函数的属性和方法。跟数学相关的运算(求绝对值,取整、最大值等)可以使用 Math 中的成员。

  1. 创建:
    特点:Math对象不用创建,直接使用。 Math.方法名();
  2. 方法:
Math.PI		 // 圆周率
Math.floor() 	 // 向下取整
Math.ceil()            // 向上取整
Math.round()           // 四舍五入版 就近取整   注意 -3.5   结果是  -3 
Math.abs()		 // 绝对值
Math.max()/Math.min()	 // 求最大和最小值 
Math.random():返回0-1之间的随机数。含0不含1
document.write(Math.PI+"<br>");//3.141592653589793
document.write(Math.random()+"<br>");
document.write(Math.round(3.14)+"<br>");//3
document.write(Math.ceil(3.14)+"<br>");//4
document.write(Math.floor(3.14)+"<br>");//3

/*
取1-100之间的随机整数
*/
document.write(Math.ceil(Math.random()*100);

随机数方法 random()

random() 方法可以随机返回一个小数,其取值范围是 [0,1),左闭右开 0 <= x < 1

得到一个两数之间的随机整数,包括两个数在内

function getRandom(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值