The creation and use of the JavaScript math object Math

Source: Internet
Author: User
Tags cos pow sin square root

The Math object in JS is not created using new, and can be used directly
Common functions include, take absolute value, round, rounding, rounding, random number, take maximum minimum
Take absolute value: Math.Abs (number)
<script type= "Text/javascript" >//Absolute value document.write ("100 absolute Value:" + math.abs ( -100) + ' <br/> ');//100</ Script>

rounding, Math.Round (number)

<script type= "Text/javascript" >//rounding document.write ("3.567 rounding:" + math.round (3.567) + ' <br/> '); 4document.write ("3.4567 rounding:" + math.round (3.4567) + ' <br/> ');//3</script>

rounding extension, preserving the specified number of decimal digits, the Math object is not used , but uses the Number Object

<script type= "Text/javascript" >//rounding extension, reserving the specified decimal place var num = 3.157892546;document.write ("3.157892546 reserved Three decimal places:" + Num.tofixed (3) + ' <br/> ');//3.158</script>

The rounding is divided into two types, one is the downward rounding, the other is the rounding (rounding up)

<script type= "Text/javascript" >//rounding is divided into two, one is to take the whole, one is to take the whole (up) document.write ("3.999999999 Downward rounding:" + Math.floor (3.999999999) + ' <br/> ');//3document.write ("3.000000001 in one rounding:" + Math.ceil (3.000000001) + ' <br/> ');//4 </script>

random a number, math.random (), random One (0,1)

<script type= "Text/javascript" >//random number, default is the random number from (0,1) document.write ("Default random Number:" + math.random () + ' <br/> ') ;//0.5043277717195451</script>

random number expansion, specifying a range of random numbers

<script type= "Text/javascript" >//the random number of the specified range document.write ("(0,100) Random number:" + (Math.random () *100) + ' <br/> ')///74.31787815876305document.write ("[0,100] random integer Between:" + (Math.ceil (Math.random () *100000000000)%101) + ' <br/ > ');d a random integer between Ocument.write ("[5,10]:" + (Math.ceil (Math.random () *100000000000)%6 + 5) + ' <br/> '); document.write ("[ -1,2] random integer Between:" + (Math.ceil (Math.random () *100000000000)%4-1) + ' <br/> ');</script>
Maximum minimum value
<script type= "Text/javascript" >//Max, Min document.write ("Maximum of two numbers:" + Math.max (10,10.5) + ' <br/> ');// 10.5document.write ("Minimum of two numbers:" + math.min ( -1,-2) + ' <br/> ');// -2//before ECMAScript V3, the method has only two parameters. document.write ("Maximum value in multiple numbers:" + Math.max (1,2,3,4,5) + ' <br/> ');//5document.write ("minimum value in multiple number:" + math.min ( 1,2,3,4,5) + ' <br/> ');//1</script>

Values commonly used in mathematics, Π,E,LN2, square root 2

<script type= "Text/javascript" >//the commonly used values in mathematics, Π,E,LN2, radical 2document.write ("PI values are:" + Math.PI + ' <br/> ');// 3.141592653589793document.write ("E value is:" + MATH.E + ' <br/> ');//2.718281828459045document.write ("LN2 value is:" + MATH.LN2 + ' <br/> ');//0.6931471805599453document.write ("The value of the square root 2 is:" + math.sqrt2 + ' <br/> ');// 1.4142135623730951</script>
Common trigonometric functions, sin (x), cos (x), tan (x)
<script type= "Text/javascript" >//commonly used trigonometric functions document.write ("sine function:" + math.sin (1) + ' <br/> ');d ocument.write ( "sine function:" + math.sin (Math.PI) + ' <br/> ');d ocument.write ("sine function:" + math.sin (MATH.PI/2) + ' <br/> '); document.write ("cosine function:" + math.cos (1) + ' <br/> ');d ocument.write ("tangent function:" + math.tan (1) + ' <br/> '); </ Script>

X y and N times, pow (x, y)

<script type= "Text/javascript" >//for X's Y-document.write ("2 of 10 times:" + Math.pow (2,10) + ' <br/> ');// 1024document.write ("square root 2 value is:" + Math.pow (2,1/2) + ' <br/> ');//1.4142135623730951//prescribe document.write (" The value of 10 of the 5-square root is: "+ Math.pow (10,1/5) + ' <br/');//1.5848931924611136</script>
Open N-Order is the 1/n power of the current number

The creation and use of the JavaScript math object Math

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.