JavaScript Number Object
JavaScript is only one numeric type.
You can use the decimal point may not be used to write numbers.
JavaScript numbers
JavaScript numbers may or may not use a decimal point to write:
Examples
var x=34; // 不使用小数点
Large or very small numbers through scientific (exponential) notation to write:
Examples
var z=123e-5; // 0.00123
All the figures are 64 JavaScript
JavaScript is not the type of language. Unlike many other programming languages, JavaScript does not define different types of digital, such as integer, short, long, float, and so on.
In JavaScript, numbers are not divided into integer type and floating-point type, all figures are from the floating-point type. JavaScript uses IEEE754 standard defines a 64-bit floating-point digital format, it can represent a maximum of ± 1.7976931348623157 x 10308, the minimum value is ± 5 x 10 -324
Value (aka Fraction / Mantissa) | index | Sign |
---|---|---|
52 bits (0 - 51) | 11 bits (50 - 62) | 1 bit (63) |
Accuracy
Integer (no decimal point or exponential notation) up to 15.
The maximum number of decimal places is 17, but the floating-point arithmetic is not always 100% accurate:
Examples
Octal and hexadecimal
If the prefix is 0, then the JavaScript will be interpreted as a numeric constant octal number, if the prefix is zero and "x", it is interpreted as a hexadecimal number.
Examples
var z = 0xFF;
Never write zeros before the number, unless you need to perform octal conversion. |
By default, JavaScript digit decimal display.
But you can use the toString () method to output hexadecimal, octal, binary.
Examples
myNumber.toString(16); // 返回 80
myNumber.toString(8); // 返回 200
myNumber.toString(2); // 返回 10000000
Infinity (Infinity)
When the digital result of the operation exceeds the number that can be represented in the upper JavaScript (overflow), the result is a special infinity (infinity) value, expressed in JavaScript to Infinity. Similarly, when a negative value exceeds the negative range of JavaScript can be represented, the result is negative infinity, in JavaScript to -Infinity indicates. Behavioral characteristics of infinite value, and we expect the same: based on their addition, subtraction, multiplication and division operation result is infinity (of course, retain their sign).
Examples
while (myNumber! = Infinity)
{
myNumber = myNumber * myNumber; // double counting until myNumber equal Infinity
}
Division by zero also had unlimited:
Examples
var y = -2/0;
NaN - non-numeric values
NaN attribute is a special value representing non-numeric values. This property is used to indicate that a value is not a number. Number objects can be set to this value, indicating that it is not a numeric value.
You can use the isNaN () global function to determine whether a value is NaN values.
Examples
isNaN(x); // 返回 true
var y = 100 / "1000";
isNaN(y); // 返回 false
Division by zero is infinity, infinity is a number:
Examples
isNaN(x); // 返回 false
Digital may be a digital or an object
Private digital data can be initialized as x = 123;
JavaScript digital object initialization data, var y = new Number (123);
Examples
var y = new Number(123);
typeof(x) // 返回 Number
typeof(y) // 返回 Object
Examples
var y = new Number(123);
(x === y) // 为 false,因为 x 是一个数字,y 是一个对象
Digital Properties
- MAX_VALUE
- MIN_VALUE
- NEGATIVE_INFINITY
- POSITIVE_INFINITY
- NaN
- prototype
- constructor
Numerical Methods
- toExponential ()
- toFixed ()
- toPrecision ()
- toString ()
- valueOf ()