First, let's take a look at The definitions of The three functions in The Definitive Guide, 4th Edition.
Math. ceil (x): round a number up
Arguments: Any numeric value or expression
Returns: The closest integer greater than or equalX.
Bytes -----------------------------------------------------------------------------------------------------
Math. floor (x): round a number down
Arguments: Any numeric value or expression
Returns: The closest integer less than or equalX.
Bytes -----------------------------------------------------------------------------------------------------
Math. round (x): round to the nearest integer
Arguments: any number.
Returns: the integer closestX.
By understanding the prototype definition of the three functions, it is easy to remember the three functions.
1. Math. Ceil () is used as the rounded up integer.
2. Math. Floor () is used for downgrading.
3. Math. Round () is used as a rounding integer.
Finally, through a specific application, we further deepen our impression on the three functions:
Suppose I want to create a web puzzle and obtain a random number in the specified range. Next I will write a custom function getrangerandom (m, n, t ).
Code
1 <script type = "text/javascript">
2 /*
3 ** function: gets a random number in a specified range.
4 */
5 function getRangeRandom (m, n, t)
6 {
7 var seed = 0;
8 switch (t)
9 {
10 // random number range: m <= seed <n
11 case0:
12 seed = m + parseInt (Math. random () * n );
13 break;
14
15 // random number range: m <= seed <n
16 case1:
17 seed = m + Math. floor (Math. random () * n );
18 break;
19
20 // random number range: m <seed <= n
21 case2:
22 seed = m + Math. ceil (Math. random () * n );
23 break;
24
25 // random number range: m <= seed <= n
26 case3:
27 seed = m + Math. round (Math. random () * n );
28 break;
29}
30
31 return seed;
32}
33 </script>