The Math object is re-organized

Source: Internet
Author: User

Note: TheMath object is an intrinsic object that you can call all of its properties and methods without having to create it, directly using math as an object. This is the difference between it and the Date,string object.

Properties of the Math object

Most of the properties that the--math object contains are some special values that might be used in mathematical calculations.


<script type= "Text/javascript" >
var Mypi=math.pi;
var myabs=math.abs (-15);
document.write (Mypi);
document.write (Myabs);
</script>

Operation Result:

3.141592653589793
15

Math Object method

A--math object that provides mathematical calculations of the data.

Math Object Method:

Math.ceil (x)

Parameter description:


Note: It returns the integer that is greater than or equal to x and closest to X. (Rounding up)

We will apply the Ceil () method to different numbers, the code is as follows:


<script type= "Text/javascript" >
document.write (Math.ceil (0.8) + "<br/>")
document.write (Math.ceil (6.3) + "<br/>")
document.write (Math.ceil (5) + "<br/>")
document.write (Math.ceil (3.5) + "<br/>")
document.write (Math.ceil ( -5.1) + "<br/>")
document.write (Math.ceil (-5.9))
</script>
Operation Result:

1
7
5
4
-5
-5

Take down the entire floor ()

The floor () method allows a number to be rounded down.

Grammar:

1
Math.floor (x)
Parameter description:

Note: Returns the integer that is less than or equal to x and closest to X.

We will use the floor () method on different numbers, with the following code:

<script type= "Text/javascript" >
document.write (Math.floor (0.8) + "<br>")
document.write (Math.floor (6.3) + "<br>")
document.write (Math.floor (5) + "<br>")
document.write (Math.floor (3.5) + "<br>")
document.write (Math.floor ( -5.1) + "<br>")
document.write (Math.floor (-5.9))
</script>

Operation Result:

0
6
5
3
-6
-6

Rounding Round ()

The round () method rounds a number to the nearest integer.

Grammar:

Math.Round (x)
Parameter description:

Attention:

1. Returns the integer closest to X.

2. For 0.5, the method will be rounded up. (5.5 will be rounded to 6)

3. If x is similar to an integer on both sides, the result is close to the numeric value in the +∞ direction. (for example, 5.5 will be rounded to-5;-5.52 will be rounded to-6), such as:

To round the different numbers to the nearest integer, the code is as follows:

<script type= "Text/javascript" >
document.write (<code>math.round (1.6) </code>+ "<br>");
document.write (<code>math.round (2.5) </code>+ "<br>");
document.write (<code>math.round (0.49) </code>+ "<br>");
document.write (<code>math.round ( -6.4) </code>+ "<br>");
document.write (<code>math.round ( -6.6) </code>);
</script>
Operation Result:

2
3
0
-6
-7

Random number ()

The random () method returns a random number between 0 and 1 (greater than or equal to 0 but less than 1).

Grammar:

1
Math.random ();

Note: Returns a numeric value that is greater than or equal to 0 but less than 1 of the symbol is positive.

We get a random number between 0 and 1, with the following code:


<script type= "Text/javascript" >
document.write (Math.random ());
</script>

Operation Result:


1
0.190305486195328

Note: Because it is a random number, the result is different for each run, but a value of 0 ~ 1.

Get a random number between 0 and 10 with the following code:


<script type= "Text/javascript" >
document.write ((Math.random ()) *10);
</script>

Operation Result:


8.72153625893887

The Math object is re-organized

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.