[Front-end technology] how to deepen understanding of Math. ceil (), Math. floor (), Math. round () functions in JavaScipt

Source: Internet
Author: User
Tags prototype definition

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>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.