Commonly used attributes

  • PI π is a partial value
Math. PI / / 3.141592653589793Copy the code

Commonly used method

ParseFloat (number); parseFloat(number); parseFloat(number);

  • Abs (number) Obtains the absolute value of the parameter
Math.abs(10)//10
Math.abs(-10)//10
Math.abs("10")//10
Math.abs("10abc")//NaN
window.parseFloat("10abc")//10
Number("10abc")//NaNCopy the code
  • Ceil (number) is rounded down
Math. Ceil (10.0001) / / 11 math.h ceil (" ABC "10.99) / / NaNCopy the code
  • Floor (number) is rounded down
Math.floor(10.2) //10 math.floor (10.99)//10 math.floor ("10.99")//10 string arguments math.floor ("10.99 ABC ")//NaNCopy the code
  • max(numberX,numberY,…) Gets the largest of the input parameters
Math.max(10,200,400)//400 math.max ("1077",200,400)//1077 note that the return value is Number, not String math.max ([1,2,3],[4,5,6]) //NaNCopy the code
  • min(numberX,numberY,…) Gets the smallest of the input parameters
Math.min(10,200,400)//10 math.min (1077,"200",400)//200 note that the return value is Number, not String math.min ([1,2,3],[4,5,6]) //NaNCopy the code
  • Pow (numberX,numberY) takes the argument x to the y power. There is no default value for argument two
Math.h pow (8, 2) / / 64 math.h pow (" 8 ", 2) / / 64 math.h pow (" 8 ", "2") / / 64 math.h pow (" 8 ") / / NaN parameters no default valueCopy the code
  • Random () returns a random number between 0 and 1
Math. The random () / / 0.636911032255739Copy the code
  • Round (number) is a method of keeping integers and a number of decimals
Math.round(10.499)//10 math.round (10.500)//11 123.43323.toFixed() //"123", which defaults to an integer when the argument is empty. Note that String is returned instead of Number 123.43323. ToFixed (2) / / "123.43" 123 toFixed (2) / / Uncaught SyntaxError: Unexpected token ILLEGAL Unexpected token ILLEGAL because the system considers the point after 123 as a decimal point instead of calling method point (123). ToFixed (2)//"123.00" , this method automatically fails when there are less than two decimal places to the decimal point, which is why the value is returned as a string instead of a numeric valueCopy the code

The above methods are the most commonly used properties and methods in Math in my opinion. The other methods are not sorted out: the w3COol -JavaScript Math object.