只要是執行算術計算都用Math
強調:Math不能new 所有的API都用大寫Math直接調用API
1.取整
上取整:Math.ceil(num)
下取整:Math.floor(num)
四舍五入:Math.round(num)
num.toFixed(d):靈活指定小數位數
,但是返回字符串 無法參與+運算
2.乘方和開平方
乘方:Nath.pow(底數,冪)
開平方:Math.sqrt(num)
3.最大值和最小值:
Math.max(值1,值2,...)
Math.min(值1,值2,...)
4.隨機數:
Math.random()生成0~1之間的隨機小數
公式:min~max之間取隨機
parseInt(Math.random()*(max-min+1)+min)