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"。

