Some commonly used methods of Math and Number built-in objects in JS
-
- Math.abs(num)
- Math.ceil(num)
- Math.floor(num)
- Math.max(num1, num2, ... , numN)
- Math.min(num1, num2, ... , numN)
- Math.pow(base, exponent)
- Math.random()
- Math.round(num)
- Math.sqrt(num)
- Number.toFixed(digits)
- Number.toString(radix)
- Number.parseFloat(string)
- Number.parseInt(string, radix)
- Number.isNaN(value)
- Number.isInteger(value)
- Number.isFinite(value)
Math.abs(num)
Parameter: num, a number whose absolute value needs to be found.
Math.ceil(num)
Parameter: num, the value that needs to be rounded up.
Note: Certain errors may occur when rounding up negative numbers.
Math.floor(num)
Parameter: num, the value that needs to be rounded down.
Math.max(num1, num2, … , numN)
Parameters: num1, num2, …, numN, the values to be compared can be any number of parameters.
Note: If the parameter passed in is empty, -Infinity will be returned; if one of the parameters cannot be converted to a number, NaN will be returned.
Math.min(num1, num2, … , numN)
Parameters: num1, num2, …, numN, the values to be compared can be any number of parameters.
Note: If the parameter passed in is empty, Infinity will be returned; if one of the parameters cannot be converted to a number, NaN will be returned.
Math.pow(base, exponent)
Parameters: base, base; exponent, exponent.
Math.random()
No parameters.
Note: The returned value range is within the interval [0,1), that is, it includes 0 but not 1.
Use in combination with Math.floor to find the range of random numbers.
Example: Randomly generate random numbers based on the input range [start, end].
Math.floor(Math.random() * (end - start + 1)) + start
Math.round(num)
Parameter: num, the value that needs to be rounded.
Math.sqrt(num)
Parameter: num, the value that needs to be squared.
Number.toFixed(digits)
Parameters: digits, an integer >= 0 and <= 20, indicating the number of decimal places to be retained in the result.
Note: If the parameter passed in is a negative number or exceeds the upper limit, a RangeError exception will be thrown.
Number.toString(radix)
Parameter: radix, indicating the base (base number) used when converting to a string, in the range of 2 ~ 36. If not specified, decimal is used by default.
Number.parseFloat(string)
Parameter: string, a string that needs to be parsed into a floating point number.
Note: Start parsing from the first character of the string until it cannot be parsed. If the first character of the string cannot be parsed as a number, NaN is returned.
Number.parseInt(string, radix)
Parameters: string, a string that needs to be parsed into an integer; radix, indicating the base (base number) used in parsing, in the range of 2 ~ 36. If not specified, decimal is used by default.
Note: Start parsing from the first character of the string until it cannot be parsed. If the first character of the string cannot be parsed as a number, NaN is returned.
Number.isNaN(value)
Parameters: value, the value to be checked.
Note: The checked value must be of Number type, and all other types will return false.
Number.isInteger(value)
Parameters: value, the value to be checked.
Note: The return value is of Boolean type. If value is an integer, it returns true; otherwise, it returns false.
Number.isFinite(value)
Parameters: value, the value to be checked.
Note: The return value is a Boolean type. If value is a finite number, it returns true; if value is NaN, Infinity or -Infinity, it returns false.