Javascript--math Object

Source: Internet
Author: User

The Math object, which provides a mathematical calculation of the data.

Using the properties and methods of Math, the code is as follows:

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

Run Results :

3.14159265358979315

Note: The Math object is an intrinsic object that you can call all of its properties and methods without creating it and directly using Math as an object. This is the difference between it and the Date,string object.

Math Object Properties

Math Object method

The above methods do not explain all, only part of the method. This section has no tasks, quickly go to the next section to learn.

Rounding up Ceil ()

The Ceil () method can be rounded up to a number.

Grammar:

Math.ceil (x)

Parameter description:

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

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:

1754-5-5

Take down the entire floor ()

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

Grammar:

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:

0653-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 ( Math.round(1.6) + "<br>");  document.write ( Math.round(2.5) + "<br>");  document.write ( Math.round(0.49) + "<br>");  document.write ( Math.round(-6.4) + "<br>");  document.write ( Math.round(-6.6) );</script>

Operation Result:

230-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:

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:

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

Javascript--math Object

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.