number.toFixed(保留小數點后幾位); 會轉為字符串類型。會自動四舍五入
Math: 數學,內置對象,保存了數學公式和信息的對象
3. Math對象上的方法
1. 找出最大值, 最小值
Math.max(多個需要比較的值); // 返回最大值
Math.min(多個需要比較的值); // 返回最小值
擴展:
Math.max.apply(Math, [5,6,7])
2. 舍入方法
Math.ceil(數字); 向上取整
Math.floor(數字); 向下取整
Math.round(數字); 四舍五入
3. 隨機數
Math.random(); 0 ~ 1;
function random(start, end) {
return Math.floor(Math.random() * (end - start + 1) + start);
}
4. 絕對值
Math.abs(數字)
5. 次冪
Math.pow(num, power): num的power次冪
6. 平方根
Math.sqrt(num); 平方根