js、Math对象

//Math对象,算数方法,全局对象,不需要使用函数进行创建
		
		console.log(typeof Math);//object
		console.dir(Math);
		console.log(Math.E);
		console.log(Math.PI);
		
		/*
		 * Math用于执行数学计算方法:
		 *  Math.ads()获取绝对值,正数的绝对是本身,负数的绝对值的正数
		 */
		console.log(Math.abs(-8));//8
		
		//Math.max()取几个数中的最大值
		//Math.min()取几个数中的最小值
		console.log(Math.max(18,19,2,20));//20
		console.log(Math.min(18,19,2,20));//2
		
		/*
		 * 小数转整数的方法: 
		 *  Math.ceil()向上舍入
		 *  Math.floor()向下舍入
		 *  Math.round()四舍五入
		 */
		//Math.ceil()向上取整,把数字向上舍入到最接近它的整数
		console.log(Math.ceil(12.1));//13
		console.log(Math.ceil(-12.1));//-12
		
		//Math.floor()向下取整,把数字向下舍入到最接近它的整数
		console.log(Math.floor(25.6));//25
		console.log(Math.floor(-25.1));//-26
		
		//Math.round()四舍五入
		console.log(Math.floor(25.6));//26
		console.log(Math.floor(-25.6));//-26
		
		/*
		 * Math.random()生成随机数
		 * Math.random()*选择范围+范围的起点
		 */
		console.log(Math.round(Math.random()*8999+1000));
		
		//Math.pow() 获取一个值的多少次幂
		console.log(Math.pow(10,3));//参数一是数组,参数二是多少次幂
		
		//Math.sqrt() 对数值开方
		console.log(Math.sqrt(Math.sqrt(100)));
		
		var num=4;
		var str="";
		(function (l){
			for(var i=0;i<l;i++){
				str+=Math.floor(Math.random()*10);
			}
		})(num);
		console.log(str);

猜你喜欢

转载自blog.csdn.net/weixin_45936690/article/details/105537493
今日推荐