JavaScript Math object

JavaScript Math (arithmetic) object

The function of the Math (arithmetic) object is to perform common arithmetic tasks.

How to use round():

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo">单击按钮舍入与“9.5”最接近的整数</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
document.getElementById("demo").innerHTML=Math.round(9.5);
}
</script>
</body>
</html>

How to use random() to return a random number between 0 and 1:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo">点击按钮显示一个随机数</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
    document.getElementById("demo").innerHTML=Math.random();
}
</script>
</body>
</html>

How to use max() to return two The larger of the given numbers. (Prior to ECMASCript v3, this method had only two parameters.):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo">单击按钮返回5到10之间的最大值。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
document.getElementById("demo").innerHTML=Math.max(5,10);
}
</script>
</body>
</html>

How to use min() to return the smaller of two given numbers. (Prior to ECMASCript v3, this method had only two parameters.)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo">单击按钮返回5到10之间最小的值。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
document.getElementById("demo").innerHTML=Math.min(5,10);
}
</script>
</body>
</html>

Math object

The purpose of the Math (arithmetic) object is to perform common arithmetic tasks.

The Math object provides a variety of arithmetic value types and functions. There is no need to define this object before using it.

Syntax for using Math properties/methods:

var x=Math.PI;
var y=Math.sqrt(16);

Note: Math The object does not need to be defined before using it.

Arithmetic values

JavaScript provides 8 arithmetic values ​​that can be accessed by the Math object:

You can refer to the following methods of using Javascript constants:

Math. E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E


Arithmetic methods

In addition to the arithmetic values ​​that can be accessed by the Math object, there are several functions (methods) that can be used.

The following example uses the round method of the Math object to round a number.

document.write(Math.round(4.7));

The output of the above code is:

5

The following example uses the Math object The random() method returns a random number between 0 and 1:

document.write(Math.random());

The output of the above code is:

0.18346685613505542

The following example uses the floor() method and random() of the Math object to return a random number between 0 and 11:

document.write (Math.floor(Math.random()*11));

The output of the above code is:

9


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo">单击按钮返回5到10之间的最大值。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ document.getElementById("demo").innerHTML=Math.max(5,10); } </script> </body> </html>
submitReset Code