Math.PI
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
值
Math.PI 屬性特性 | |
|---|---|
| 可寫 | 否 |
| 可列舉 | 否 |
| 可配置 | 否 |
描述
因為 PI 是 Math 的靜態屬性,所以你總是透過 Math.PI 來使用它,而不是透過你自己建立的 Math 物件屬性來使用(Math 不是一個建構函式)。
示例
使用 Math.PI
下面的函式使用 Math.PI 來計算給定半徑的圓的周長。
js
function calculateCircumference(radius) {
return Math.PI * (radius + radius);
}
calculateCircumference(1); // 6.283185307179586
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-math.pi |
瀏覽器相容性
載入中…