亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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中文網(wǎng)(php.cn)</title>
</head>
<body>
<p id="demo">單擊按鈕舍入與“9.5”最接近的整數(shù)</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中文網(wǎng)(php.cn)</title>
</head>
<body>
<p id="demo">點擊按鈕顯示一個隨機數(shù)</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中文網(wǎng)(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中文網(wǎng)(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中文網(wǎng)(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