JavaScript Math 对象允许您执行数学任务 数字。
Math.PI;
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.PI</h2>
<p>Math.PI returns the ratio of a circle's circumference to its diameter:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.PI;
</script>
</body>
</html>
与其他对象不同,Math 对象没有构造函数。
Math 对象是静态的。
无需先创建 Math 对象即可使用所有方法和属性。
任何 Math 属性的语法都是:Math.property
。
JavaScript 提供了 8 个可以作为数学属性访问的数学常量:
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math Constants</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"<p><b>Math.E:</b> " + Math.E + "</p>" +
"<p><b>Math.PI:</b> " + Math.PI + "</p>" +
"<p><b>Math.SQRT2:</b> " + Math.SQRT2 + "</p>" +
"<p><b>Math.SQRT1_2:</b> " + Math.SQRT1_2 + "</p>" +
"<p><b>Math.LN2:</b> " + Math.LN2 + "</p>" +
"<p><b>Math.LN10:</b> " + Math.LN10 + "</p>" +
"<p><b>Math.LOG2E:</b> " + Math.LOG2E + "</p>" +
"<p><b>Math.Log10E:</b> " + Math.LOG10E + "</p>";
</script>
</body>
</html>
Math 任何方法的语法为: Math.method(number)
有 4 种常用方法将数字舍入为整数:
返回 x 四舍五入到最接近的整数
返回 x 四舍五入到最接近的整数
返回 x 向下舍入到最接近的整数
返回 x 的整数部分(ES6 中的新功能)
Math.round()
Math.round(x)
返回最接近的整数:
Math.round(4.6);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.6);
</script>
</body>
</html>
Math.round(4.5);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.5);
</script>
</body>
</html>
Math.round(4.4);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.round()</h2>
<p>Math.round(x) returns the value of x rounded to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.round(4.4);
</script>
</body>
</html>
Math.ceil()
Math.ceil(x)
返回 x 的值,向上舍入到最接近的整数:
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.ceil()</h2>
<p>Math.ceil() rounds a number <strong>up</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.ceil(4.4);
</script>
</body>
</html>
Math.floor()
Math.floor(x)
返回 x 的值,向下舍入到最接近的整数:
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.floor()</h2>
<p>Math.floor(x) returns the value of x rounded <strong>down</strong> to its nearest integer:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(4.7);
</script>
</body>
</html>
Math.trunc()
Math.trunc(x)
返回 x 的整数部分:
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.trunc()</h2>
<p>Math.trunc(x) returns the integer part of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.trunc(4.7);
</script>
</body>
</html>
Math.sign()
Math.sign(x)
如果 x 为负数、空值或正数,则返回:
Math.sign(-4);
Math.sign(0);
Math.sign(4);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sign()</h2>
<p>Math.sign(x) returns if x is negative, null or positive:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sign(4);
</script>
</body>
</html>
Math.trunc() 和 Math.sign() 已添加到 JavaScript 2015 - ES6。
Math.pow()
Math.pow(x, y)
返回 x 的 y 次方值:
Math.pow(8, 2);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.pow()</h2>
<p>Math.pow(x,y) returns the value of x to the power of y:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.pow(8,2);
</script>
</body>
</html>
Math.sqrt()
Math.sqrt(x)
返回 x 的平方根:
Math.sqrt(64);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sqrt()</h2>
<p>Math.sqrt(x) returns the square root of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.sqrt(64);
</script>
</body>
</html>
Math.abs()
Math.abs(x)
返回 x 的绝对(正)值:
Math.abs(-4.7);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.abs()</h2>
<p>Math.abs(x) returns the absolute (positive) value of x:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.abs(-4.7);
</script>
</body>
</html>
Math.sin()
Math.sin(x)
返回角度 x(以弧度给出)的正弦值(-1 到 1 之间的值)。
如果您想使用度数而不是弧度,则必须将度数转换为弧度:
角度(弧度)= 角度(度数)x PI/180。
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.sin()</h2>
<p>Math.sin(x) returns the sin of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The sine value of 90 degrees is " + Math.sin(90 * Math.PI / 180);
</script>
</body>
</html>
Math.cos()
Math.cos(x)
返回角度 x(以弧度给出)的余弦(-1 到 1 之间的值)。
如果您想使用度数而不是弧度,则必须将度数转换为弧度:
角度(弧度)= 角度(度数)x PI/180。
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.cos()</h2>
<p>Math.cos(x) returns the cosine of x (given in radians):</p>
<p>Angle in radians = (angle in degrees) * PI / 180.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The cosine value of 0 degrees is " + Math.cos(0 * Math.PI / 180);
</script>
</body>
</html>
Math.min() / Math.max()
Math.min()
和 Math.max()
可用于查找以下值的最低或最高值参数列表:
Math.min(0, 150, 30, 20, -8, -200);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.min()</h2>
<p>Math.min() returns the lowest value in a list of arguments:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.min(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>
Math.max(0, 150, 30, 20, -8, -200);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.max()</h2>
<p>Math.max() returns the highest value in a list of arguments.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.max(0, 150, 30, 20, -8, -200);
</script>
</body>
</html>
Math.random()
Math.random()
返回 0(含)和 1 之间的随机数 (独家的):
Math.random();
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 and 1:</p>
<p id="demo"></p>
<p>Tip: Click on "Run" several times.</p>
<script>
document.getElementById("demo").innerHTML = Math.random();
</script>
</body>
</html>
您将在本教程的下一章中了解有关 Math.random()
的更多信息。
Math.log(x)
返回 x 的自然对数。
自然对数返回达到一定增长水平所需的时间:
Math.log(1);
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(1);
</script>
</body>
</html>
Math.log(2);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(2);
</script>
</body>
</html>
Math.log(3);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>Math.log() returns the natural logarithm of a number:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(3);
</script>
</body>
</html>
Math.E 和 Math.log() 是双胞胎。
我们必须将 Math.E 乘以多少次才能得到 10?
Math.log(10);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log()</h2>
<p>How many times must we multiply Math.E to get 10?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log(10);
</script>
</body>
</html>
Math.log2(x)
返回 x 以 2 为底的对数。
2 必须乘多少次才能得到 8?
Math.log2(8);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log2()</h2>
<p>Math.log2() returns the base 2 logarithm of a number.</p>
<p>How many times must we multiply 2 to get 8?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log2(8);
</script>
</body>
</html>
Math.log10(x)
返回 x 以 10 为底的对数。
我们必须乘以 10 多少次才能得到 1000?
Math.log10(1000);
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.log10()</h2>
<p>Math.log10() returns the base 10 logarithm of a number.</p>
<p>How many times must we multiply 10 to get 1000?</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.log10(1000);
</script>
</body>
</html>
返回 x 的绝对值
返回 x 的反余弦(以弧度为单位)
返回 x 的双曲反余弦
返回 x 的反正弦,以弧度为单位
返回 x 的双曲反正弦
以 -PI/2 和 PI/2 弧度之间的数值形式返回 x 的反正切值
返回其参数商的反正切值
返回 x 的双曲反正切值
返回 x 的立方根
返回 x,向上舍入到最接近的整数
返回 x 的余弦(x 以弧度为单位)
返回 x 的双曲余弦
返回 Ex 的值
返回 x,向下舍入到最接近的整数
返回 x 的自然对数(以 E 为底)
返回具有最高值的数字
返回具有最小值的数字
返回 x 的 y 次方值
返回 0 到 1 之间的随机数
将 x 四舍五入到最接近的整数
返回 x 是否为负、空或正 (-1, 0, 1)
返回 x 的正弦值(x 的单位是弧度)
返回 x 的双曲正弦值
返回 x 的平方根
返回角度的正切值
返回数字的双曲正切
返回数字 (x) 的整数部分
如需完整参考,请访问我们的完整数学对象参考。
该参考包含所有数学属性和方法的描述和示例。