JavaScript Math 對象

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

使用 Math 的屬性和方法的語法:

var pi_value=Math.PI;
var sqrt_value=Math.sqrt(15);

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

Math 對象屬性

屬性 描述
E 返回算術常量 e,即自然對數的底數(約等于2.718)。
LN2 返回 2 的自然對數(約等于0.693)。
LN10 返回 10 的自然對數(約等于2.302)。
LOG2E 返回以 2 為底的 e 的對數(約等于 1.414)。
LOG10E 返回以 10 為底的 e 的對數(約等于0.434)。
PI 返回圓周率(約等于3.14159)。
SQRT1_2 返回返回 2 的平方根的倒數(約等于 0.707)。
SQRT2 返回 2 的平方根(約等于 1.414)。

Math 對象方法

方法 描述
abs(x) 返回數的絕對值。
acos(x) 返回數的反余弦值。
asin(x) 返回數的反正弦值。
atan(x) 以介于 -PI/2 與 PI/2 弧度之間的數值來返回 x 的反正切值。
atan2(y,x) 返回從 x 軸到點 (x,y) 的角度(介于 -PI/2 與 PI/2 弧度之間)。
ceil(x) 對數進行上舍入。
cos(x) 返回數的余弦。
exp(x) 返回 e 的指數。
floor(x) 對數進行下舍入。
log(x) 返回數的自然對數(底為e)。
max(x,y) 返回 x 和 y 中的最高值。
min(x,y) 返回 x 和 y 中的最低值。
pow(x,y) 返回 x 的 y 次冪。
random() 返回 0 ~ 1 之間的隨機數。
round(x) 把數四舍五入為最接近的整數。
sin(x) 返回數的正弦。
sqrt(x) 返回數的平方根。
tan(x) 返回角的正切。

abs() 方法可返回數的絕對值。

語法

Math.abs(x)

返回值

x 的絕對值。

取得正數和負數的絕對值:

console.log(Math.abs(7.25))//7.25
console.log(Math.abs(-7.25))//7.25
console.log(Math.abs(7.25-10))//2.75

acos() 方法可返回一個數的反余弦。

語法

Math.acos(x)

返回值

x 的反余弦值。返回的值是 0 到 PI 之間的弧度值。

<script type="text/javascript">

document.write(Math.acos(0.64) + "<br />")
document.write(Math.acos(0) + "<br />")
document.write(Math.acos(-1) + "<br />")
document.write(Math.acos(1) + "<br />")
document.write(Math.acos(2))

</script>
0.8762980611683406
1.5707963267948965
3.141592653589793
0
NaN

asin() 方法可返回一個數的反正弦值。

語法

Math.asin(x)

返回值

x 的反正弦值。返回的值是 -PI/2 到 PI/2 之間的弧度值。

<script type="text/javascript">

document.write(Math.asin(0.64) + "<br />")
document.write(Math.asin(0) + "<br />")
document.write(Math.asin(-1) + "<br />")
document.write(Math.asin(1) + "<br />")
document.write(Math.asin(2))

</script>
0.6944982656265559
0
-1.5707963267948965
1.5707963267948965
NaN

ceil() 方法可對一個數進行上舍入。

語法

Math.ceil(x)

返回值

大于等于 x,并且與它最接近的整數。

<script type="text/javascript">

document.write(Math.ceil(0.60) + "<br />")
document.write(Math.ceil(0.40) + "<br />")
document.write(Math.ceil(5) + "<br />")
document.write(Math.ceil(5.1) + "<br />")
document.write(Math.ceil(-5.1) + "<br />")
document.write(Math.ceil(-5.9))

</script>
1
1
5
6
-5
-5

floor() 方法可對一個數進行下舍入

語法

Math.floor(x)

返回值

小于等于 x,且與 x 最接近的整數。

<script type="text/javascript">

document.write(Math.floor(0.60) + "<br />")
document.write(Math.floor(0.40) + "<br />")
document.write(Math.floor(5) + "<br />")
document.write(Math.floor(5.1) + "<br />")
document.write(Math.floor(-5.1) + "<br />")
document.write(Math.floor(-5.9))

</script>
0
0
5
5
-6
-6

max() 方法可返回兩個指定的數中帶有較大的值的那個數。

語法

Math.max(x...)

返回值

參數中最大的值。如果沒有參數,則返回 -Infinity。如果有某個參數為 NaN,或是不能轉換成數字的非數字值,則返回 NaN。

<script type="text/javascript">

document.write(Math.max(5,7) + "<br />")
document.write(Math.max(-3,5) + "<br />")
document.write(Math.max(-3,-5) + "<br />")
document.write(Math.max(7.25,7.30))

</script>
7
5
-3
7.3

min() 方法可返回指定的數字中帶有最低值的數字。

語法

Math.min(x,y)

返回值

參數中最小的值。如果沒有參數,則返回 Infinity。如果有某個參數為 NaN,或是不能轉換成數字的非數字值,則返回 NaN。

<script type="text/javascript">

document.write(Math.min(5,7) + "<br />")
document.write(Math.min(-3,5) + "<br />")
document.write(Math.min(-3,-5) + "<br />")
document.write(Math.min(7.25,7.30))

</script>
5
-3
-5
7.25

pow() 方法可返回 x 的 y 次冪的值。

語法

Math.pow(x,y)

返回值

x 的 y 次冪。

<script type="text/javascript">

document.write(Math.pow(0,0) + "<br />")
document.write(Math.pow(0,1) + "<br />")
document.write(Math.pow(1,1) + "<br />")
document.write(Math.pow(1,10) + "<br />")
document.write(Math.pow(2,3) + "<br />")
document.write(Math.pow(-2,3) + "<br />")
document.write(Math.pow(2,4) + "<br />")
document.write(Math.pow(-2,4) + "<br />")

</script>
1
0
1
1
8
-8
16
16

random() 方法可返回介于 0 ~ 1 之間的一個隨機數。

語法

Math.random()

返回值

0.0 ~ 1.0 之間的一個偽隨機數。

<script type="text/javascript">

document.write(Math.random())

</script>
0.3546745664884482

round() 方法可把一個數字舍入為最接近的整數。

語法

Math.round(x)

返回值

與 x 最接近的整數。

<script type="text/javascript">

document.write(Math.round(0.60) + "<br />")
document.write(Math.round(0.50) + "<br />")
document.write(Math.round(0.49) + "<br />")
document.write(Math.round(-4.40) + "<br />")
document.write(Math.round(-4.60))
1
1
0
-4
-5
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容