此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Math.PI

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

Math.PI 静态数据属性表示圆的周长与直径的比例,其值约为 3.14159。

尝试一下

function calculateCircumference(radius) {
  return 2 * Math.PI * radius;
}

console.log(Math.PI);
// Expected output: 3.141592653589793

console.log(calculateCircumference(10));
// Expected output: 62.83185307179586

𝙼𝚊𝚝𝚑.𝙿𝙸=π3.14159\mathtt{Math.PI} = \pi \approx 3.14159
Math.PI 的属性特性
可写
可枚举
可配置

描述

由于 PIMath 的静态属性,因此应始终使用 Math.PI,而不是作为你创建的 Math 对象的属性(Math 不是构造函数)。

示例

使用 Math.PI

以下函数使用 Math.PI 来计算给定半径的圆周长。

js
function calculateCircumference(radius) {
  return Math.PI * (radius + radius);
}

calculateCircumference(1); // 6.283185307179586

规范

规范
ECMAScript® 2027 Language Specification
# sec-math.pi

浏览器兼容性

参见