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 ....