JavaScript Number object
Introduction
Number object is a numerical object, including integers, floating point numbers, etc. in js.
Definition
var a = 1;
var b = 1.1;
Attribute
##1 Number.MAX_VALUE: Represents the largest value in JS Number, approximately 1.79e+3082 Number.MIN_VALUE: Represents the smallest number in JS, approximately 5e-3243 Number.NaN: Returns NaN, indicating a non-numeric value, Not equal to any other number, including NaN itself. Number.isNaN() should be used to judge. 4 Number.NEGATIVE_INFINITY: Returns -Infinity, indicating negative infinity. 5 Number.POSITIVE_INFINITY: Returns Infinity, indicating positive infinity. If the calculated value is greater than Number.MAX_VALUE, Infinity is returned.
Method
##1 Number.isInteger(value): Determine whether the parameter is an integer
Parameters:
①value {Number}: number
Return value:
{Boolean} Returns whether the parameter is an integer. Pure integer strings also return false.
Example:
Number.isInteger(1); // => true
Number.isInteger(1.1); // => false
Number.isInteger('1'); // => false: Pure integer strings also return false
Number.isInteger('1.1'); // => false
Number.isInteger('a'); // => false: non-string returns false
2 Number.isNaN(value): determines whether the parameter is NaN
parameter :
①value {Object} : Any type
Return value:
{Boolean} Whether the return parameter is NaN.
Example:
Number.isNaN(NaN); // => true
Number.isNaN('NaN'); // => false :' NaN' string is not NaN
Number.isNaN(1); // => false
Number.isNaN('1'); // => false
3 Number.parseFloat(value): Convert the parameter to a floating point number
Parameters:
①value {Number | NumberStr}: Number or pure numeric string
return value:
{Integer | Float} Returns an integer or floating point number
Example:
Number.parseFloat(1); // => 1: Integer or returns an integer
Number.parseFloat(1.1); // => 1.1
Number.parseFloat('1aaa'); // => 1: If the string is preceded by a number, only numbers are returned
Number.parseFloat('1.1aaa'); // => 1.1
Number.parseFloat('a1'); // => NaN: does not start with a number, returns NaN
Number.parseFloat('a'); // => NaN
4 Number.parseInt(value): Convert the parameter to an integer
Parameter:
①value {Number | NumberStr}: Number or pure numeric string
Return value:
{Integer} Return integer value
Example:
Number.parseInt(1); // => 1
Number.parseInt(1.1); // => 1: Floating point number returns integer
Number.parseInt('1aaa' ); // => 1: If the string is preceded by a number, only numbers are returned
Number.parseInt('1.1aaa'); // => 1
Number.parseInt ('a1'); // => NaN: does not start with a number, returns NaN
Number.parseInt('a'); // => NaN
Octal and Hexadecimal
If the prefix is 0, JavaScript will interpret the numeric constant as an octal number. If the prefix is 0 and "x", it will be interpreted as a hexadecimal number. .
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var y = 0377; var z = 0xFF; document.write(y + "<br>"); document.write(z + "<br>"); </script> </body> </html>
By default, JavaScript numbers are displayed in decimal.
But you can use the toString() method to output hexadecimal, octal, or binary.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var myNumber = 28; document.write(myNumber + ' 十进制<br>'); document.write(myNumber.toString(16) + ' 十六进制<br>'); document.write(myNumber.toString(8) + ' 八进制<br>'); document.write(myNumber.toString(2) + ' 二进制<br>'); </script> </body> </html>
Infinity
When the numerical operation result exceeds the upper limit of the number that JavaScript can represent (overflow), the result is a special infinity value, which is represented by Infinity in JavaScript express. Similarly, when the value of a negative number exceeds the range of negative numbers that JavaScript can represent, the result is negative infinity, which is represented by -Infinity in JavaScript. Infinite values behave as we would expect: operations based on their addition, subtraction, multiplication, and division still result in infinity (while retaining their signs, of course).
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> myNumber=2; while (myNumber!=Infinity){ myNumber=myNumber*myNumber; document.write(myNumber +'<BR>'); } </script> </body> </html>
Number attributes
MAX_VALUE
MIN_VALUE
NEGATIVE_INFINITY
POSITIVE_INFINITY
NaN
prototype
constructor
数字方法
toExponential()
toFixed()
toPrecision()
toString()
valueOf()