JavaScript內置對象:math

Math對象:math對象用于執行數學任務。

注意:Math 對象并不像 Date 和 String 那樣是對象的類,因此沒有構造函數 Math(),像 Math.sin() 這樣的函數只是函數,不是某個對象的方法。您無需創建它,通過把 Math 作為對象使用就可以調用其所有屬性和方法。

Math的內置方法:

  1. math.ceil(x) 可以對x進行向上取整
dacument.write(math.ceil(7.9)
//結果:8
dacument.write(math.ceil(-7.9)
//結果:-7
  1. math.floor() 可以對數進行向下取整
dacument.write(math.floor(7.9)
//結果:7
dacument.write(math.floor(-7.9)
//結果:-8
  1. math.max(x,y)返回其中的最大值
dacument.write(math.max(7,9))
//結果:9
dacument.write(math.max(-7,-9)
//結果:-7
  1. math.min() 返回其中最小值
//結果:7
dacument.write(math.min(7,-9))
//結果:-9
  1. Math.random() 隨機生成0~1之間的隨機數
dacument.write(math.random()*10)
//隨機生成0-10之間的數
//生成X-y之間的數
X+document.write(math.random()*(y-x))
  1. Math.round(x) 把x四舍五入為最近的整數
dacument.write(math.round(4.5))
//結果:5
  1. math.pow(x,y) 返回x的y次冪
dacument.write(math.pow(2,2))
//結果:4
  1. Math.abs() 返回數的絕對值
dacument.write(math.abs(-8))
//結果:8
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容