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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章