The math object of JavaScript's built-in objects

Source: Internet
Author: User
Tags rounds

First, the Math object Overview:

The purpose of the math (arithmetic) object is to: perform common arithmetic tasks. Save mathematical formulas and information.

The Math object provides a much faster execution of the computational functionality than we do with the direct programming of JavaScript.

Attention:

The Math object is not a class of objects like Date and String, so there is no constructor for math (), a function like Math.sin () is just a function, not a method of an object.

You do not have to create it, and you can invoke all of its properties and methods by using Math as an object.

Second, the properties of the Math object:

The Math object contains properties that are mostly special values that may be used in mathematical calculations.

alert (MATH.E);                         alert (MATH.LN10);                    alert (MATH.LN2);                    alert (math.log2e);    alert (math.log10e);    alert (Math.PI);    alert (math.sqrt1_2);    alert (MATH.SQRT2);

Third, the method of the Math object:

1.min () and Max () methods :

Math.min () is used to determine the minimum value in a set of values.

Math.max () is used to determine the maximum value in a set of values.

    //Max () methoddocument.write (Math.max (5,7) + "<br/>"); document.write (Math.max (-3,5) + "<br/>"); document.write (Math.max (-3,-5) + "<br/>"); document.write (Math.max (7.25,7.30)); //min () methoddocument.write (Math.min (5,7) + "<br/>"); document.write (Math.min (-3,5) + "<br/>"); document.write (Math.min (-3,-5) + "<br/>"); document.write (Math.min (7.25,7.30)); Alert (Math.min (2,4,3,6,3,8,0,1,3));//Minimum ValueAlert (Math.max (4,7,8,3,1,9,6,0,3,2));//Maximum Value

2. Rounding Method
Math.ceil () performs an upward rounding, that is, it always rounds the value up to the nearest integer;

    //  -    //  -    //  -

Math.floor () Performs a downward rounding, that is, it always rounds the value down to the nearest integer;

    //  -    //  -    //  -

Math.Round () performs a standard rounding, that is, it always rounds a value to the nearest integer;

alert (Math.Round (25.9)); // -alert (Math.Round (25.5)); // -alert (Math.Round (25.1)); // -

3.random () Method:

The method returns a random decimal between 0 and 1, excluding 0 and 1.

Alert (Math.random ());

If you want to get a random number larger than this range, you can apply the formula: value = Math.floor (Math.random () * total + first value)

 //  randomly generates any number between 1-10  //  first get random decimals  var  box = Math.random ();  //  The random decimal number to be obtained, multiplied by 10 until the decimal between 0 and 10, excluding 0 and 10,        Finally add 1 to wait until the decimal between 1 and 10  box = Box*10+1;  //  truncate the trailing decimals (rounding the values down) to an integer  box    = Math.floor (box);        alert (box);  //  written in one sentence: Alert (Math.floor ( Math.random () * 10 + 1)); 
 for (var i = 0; I<10;i + +)        any number between{//5-14 10+5-1=14        document.write (' <br /> ');    }     // If you want 5 to 10 10-5+1 = 6 is *6+5

For more convenient delivery of the desired range, a function can be defined:

function Selectfrom (lower, Upper) {        var sum = upper-lower + 1;                                 // Total-Number of first +1        return Math.floor (math.random () * sum + lower);    }          for (var i=0; i<10;i++) {        document.write (Selectfrom (5,100));                             // direct delivery range can        be document.write (' <br/> ');    }

4. Other methods

  

The math object of JavaScript's built-in objects

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.