Javascript basics of the Math object __java

Source: Internet
Author: User
Tags mathematical functions pow
Math Object

The Math object provides a large number of effective mathematical functions and numerical manipulation methods.

The Math object is a little different because JavaScript automatically creates it, and you don't need to declare a variable as a math object or define a new math object before using the Math object, so it's easy to use the Math object.

var value=math.pi*15*15;
document.write (value); Output the area of a circle
methods of the Math object

ABS () method

Returns the absolute value of a number.

document.write (Math.Abs (-7.25))//Output 7.25

Min () and Max () methods ———— find the maximum and minimum values

There are two numbers to determine which is larger and which is smaller, for this reason, the Math object provides the min () and Max () methods.

document.write (Math.max ( -3,-5) + "<br/>")//3
document.write (Math.min ( -3,-5) + "<br/>")/5

Rounding Method

The Math object provides several numeric rounding methods, each of which has its own purpose.

Ceil () method

The Ceile () method always takes the value up,

document.write (Math.ceil (5.1) + "<br/>")/output 6
document.write (Math.ceil ( -5.1) + "<br/>")/output-5

Floor () method

The floor () method can be rounded down for a number.

document.write (Math.floor (5.1) + "<br/>")/output 5
document.write (Math.floor ( -5.1) + "<br/>")/output-6

Round () method

Rounding in mathematics, when the decimal part is greater than or equal to 0.5 is up, and is less than 0.5 when downward.

document.write (Math.Round (45.5))//Output
document.write (Math.Round (45.49))//Output 45

Summary of Math.Round, parseint, Math.floor and Math.ceil decimal rounding

Random () method

The random () method can return a random number between 0 ~ 1.

var count;
for (var i=0;i< 6;i++) {
   count = (Math.floor (math.random () *6) +1);
  document.write (count + "<br/>")
}

In the code above, use the random method to return a random floating-point number between a 0~1 (not including 1). Multiply this number by 6, get a random number between 0~6 (not 6), then add 1, get a 1~7 (not 7) random number, and then trim it down with the floor () method. Then we get a 1~6 (6) random number, which is a small game to roll the dice code, can be more perfect, to more interesting.

Pow () method

The POW () method returns the value of the Y-power of X.

document.write (Math.pow (2,4))/output
document.write (Math.pow ( -2,4))//Output 16

The math method also has some methods to stop repeating again, interested can refer to the consortium
Document for review.

Reprint please indicate the source ....

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.