JavaScript Math.PI



符號(π)源自希臘語“periphereia”,意為“周長”或“圓周”。在數學上,PI(π)表示圓的周長與其直徑之比。π 的值約為 3.14159。換句話說,π (3.14159) = 圓的周長 / 圓的直徑。

JavaScript 的Math.PI屬性表示圓的周長與其直徑之比,約為 3.14。

語法

以下是 JavaScript Math.PI 屬性的語法:

Math.PI

返回值

此屬性返回 PI 的值。

示例 1

在以下示例中,我們使用 JavaScript Math.PI 屬性計算圓的周長:

<html>
<body>
<script>
   const radius = 5;
   const circumference = 2 * Math.PI * radius;
   document.write(circumference);
</script>
</body>
</html>

輸出

如果我們執行上述程式,它將返回圓的周長“31.41592653589793”。

示例 2

在這裡,我們使用 Math.PI 屬性計算圓的面積:

<html>
<body>
<script>
   const radius = 3;
   const area = Math.PI * radius * radius;
   document.write(area);
</script>
</body>
</html>

輸出

執行後,圓的面積將為“28.274333882308138”。

示例 3

在此示例中,我們使用 Math.PI 屬性計算圓的直徑:

<html>
<body>
<script>
   const circumference = 15;
   const diameter = circumference / Math.PI;
   document.write(diameter);
</script>
</body>
</html>

輸出

如果我們執行上述程式,它將返回圓的直徑“4.7746482927568605”。

示例 4

這裡,我們使用 Math.PI 屬性計算弧長:

<html>
<body>
<script>
   const radius = 4;
   const angleInRadians = Math.PI / 3; 
   const arcLength = radius * angleInRadians;
   document.write(arcLength);
</script>
</body>
</html>

輸出

弧長將為“4.1887902047863905”。

廣告