JavaScript 数学对象


目录

    显示目录

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 种常用方法将数字舍入为整数:

Math.round(x)

返回 x 四舍五入到最接近的整数

Math.ceil(x)

返回 x 四舍五入到最接近的整数

Math.floor(x)

返回 x 向下舍入到最接近的整数

Math.trunc(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() 方法

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() 方法

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() 方法

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>

JavaScript 数学方法

abs(x)

返回 x 的绝对值

acos(x)

返回 x 的反余弦(以弧度为单位)

acosh(x)

返回 x 的双曲反余弦

asin(x)

返回 x 的反正弦,以弧度为单位

asinh(x)

返回 x 的双曲反正弦

atan(x)

以 -PI/2 和 PI/2 弧度之间的数值形式返回 x 的反正切值

atan2(y, x)

返回其参数商的反正切值

atanh(x)

返回 x 的双曲反正切值

cbrt(x)

返回 x 的立方根

ceil(x)

返回 x,向上舍入到最接近的整数

cos(x)

返回 x 的余弦(x 以弧度为单位)

cosh(x)

返回 x 的双曲余弦

exp(x)

返回 Ex 的值

floor(x)

返回 x,向下舍入到最接近的整数

log(x)

返回 x 的自然对数(以 E 为底)

max(x, y, z, ..., n)

返回具有最高值的数字

min(x, y, z, ..., n)

返回具有最小值的数字

pow(x, y)

返回 x 的 y 次方值

random()

返回 0 到 1 之间的随机数

round(x)

将 x 四舍五入到最接近的整数

sign(x)

返回 x 是否为负、空或正 (-1, 0, 1)

sin(x)

返回 x 的正弦值(x 的单位是弧度)

sinh(x)

返回 x 的双曲正弦值

sqrt(x)

返回 x 的平方根

tan(x)

返回角度的正切值

tanh(x)

返回数字的双曲正切

trunc(x)

返回数字 (x) 的整数部分

完整的数学参考

如需完整参考,请访问我们的完整数学对象参考。

该参考包含所有数学属性和方法的描述和示例。