符号 (π) 源自希腊语“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.27433382308138”。
示例 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”。