JavaScript Number object
JavaScript has only one number type.
Numbers can be written with or without a decimal point.
JavaScript Numbers
JavaScript numbers can be written with or without a decimal point:
Example
var pi=3.14; // Use Decimal point
var x=34; // Do not use decimal point
Very large or very small numbers can be written using scientific (exponential) notation:
Example
var y=123e5; // 12300000
var z=123e-5; // 0.00123
All JavaScript numbers are 64-bit
JavaScript is not a typed language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integer, short, long, floating point, etc.
In JavaScript, numbers are not divided into integer types and floating-point types. All numbers are composed of floating-point types. JavaScript uses the 64-bit floating point format defined by the IEEE754 standard to represent numbers. It can represent a maximum value of ±1.7976931348623157 x 10308 and a minimum value of ±5 x 10 -324
Precision
Integers (without decimal point or exponent notation) can be up to 15 digits.
The maximum number of decimal digits is 17, but floating point operations are not always 100% accurate:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> var x; document.write("<p>仅显示17位: "); x=12345678901234567890; document.write(x + "</p>"); document.write("<p>0.2 + 0.1 = "); x=0.2+0.1; document.write(x + "</p>"); document.write("<p>可以通过乘以10或除以10来计算: "); x=(0.2*10+0.1*10)/10; document.write(x +"</p>"); </script> </body> </html>
Octal and Hexadecimal
If the prefix is 0, JavaScript will interpret the numeric constant as an octal number, and if the prefix is 0 and "x", it will be interpreted as a hexadecimal number.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> var y = 0377; var z = 0xFF; document.write(y + "<br>"); document.write(z + "<br>"); </script> </body> </html>
Never write zeros in front of numbers unless you need to do an octal conversion.
By default, JavaScript numbers are displayed in decimal.
But you can use the toString() method to output hexadecimal, octal, or binary.
Infinity
When the numerical operation result exceeds the upper limit of numbers that JavaScript can represent (overflow), the result is a special infinity (infinity ) value, represented by Infinity in JavaScript. 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"> </head> <body> <script> myNumber=2; while (myNumber!=Infinity){ myNumber=myNumber*myNumber; document.write(myNumber +'<BR>'); } </script> </body> </html>
NaN - Non-numeric value
The NaN attribute is a special value that represents a non-numeric value. This attribute is used to indicate that a value is not a number. A Number object can be set to this value to indicate that it is not a numeric value.
You can use the isNaN() global function to determine whether a value is a NaN value.
Numbers can be numbers or objects
Numbers can be initialized with private data, like x = 123;
JavaScript Digital object initialization data, var y = new Number(123);
Number attribute
##MAX_VALUEMIN_VALUENEGATIVE_INFINITYPOSITIVE_INFINITYNaNprototypeconstructorNumber methodstoExponential()toFixed ()toPrecision()toString()valueOf()