JavaScript Advanced--(JavaScript built-in object---)--math Object---Note finishing

Source: Internet
Author: User

Math Object
Use the properties and methods of Math:

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

Operation Result:

3.14159265358979315

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

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

X required. Must be a numeric value.

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

1 <script type= "Text/javascript" >2     document.write (Math.ceil (0.8) + "<br/>")  3     document.write (Math.ceil (6.3) + "<br/>")4     document.write ( Math.ceil (5) + "<br/>")5     document.write (Math.ceil (3.5) + "<br/>") 6     document.write (Math.ceil ( -5.1) + "<br/>")7     document.write (Math.ceil ( -5.9))  8 </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)  x required, must be a value.

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

1 <script type= "Text/javascript" >2     document.write (Math.floor (0.8) + "<br>")  3     document.write (Math.floor (6.3) + "<br>")4     document.write ( Math.floor (5) + "<br>")5     document.write (Math.floor (3.5) + "<br>") 6     document.write (Math.floor ( -5.1) + "<br>")7     document.write (Math.floor ( -5.9) )8 </script>    

Operation Result:

0653-6-6

Rounding Round ()
The round () method rounds a number to the nearest integer.
Grammar:

X required. Must be a number

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:

1 <script type= "Text/javascript" >2     document.write (Math.Round (1.6) + "<br>"); 3     document.write (Math.Round (2.5) + "<br>"); 4     document.write (Math.Round (0.49) + "<br>"); 5     document.write (Math.Round ( -6.4) + "<br>"); 6     document.write (Math.Round ( -6.6)); 7 </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 ();

Returns a numeric value that is positive for a symbol greater than or equal to 0 but less than 1.


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

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

Operation Result:

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 Advanced--(under---of JavaScript built-in objects)--math objects---Note finishing

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.