Latest web development tutorials

# 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 pi=3.14; // 使用小数点
var x=34; // 不使用小数点

Large or very small numbers through scientific (exponential) notation to write:

## Examples

var y=123e5; // 12300000
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

var x = 0.2+0.1; // 输出结果为 0.30000000000000004

try it"

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 y = 0377;
var z = 0xFF;

try it"

 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

var myNumber=128;
myNumber.toString(16); // 返回 80
myNumber.toString(8); // 返回 200
myNumber.toString(2); // 返回 10000000

try it"

## 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

myNumber = 2;
while (myNumber! = Infinity)
{
myNumber = myNumber * myNumber; // double counting until myNumber equal Infinity
}

try it"

Division by zero also had unlimited:

var x = 2/0;
var y = -2/0;

try it"

## 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

var x = 1000 / "Apple";
isNaN(x); // 返回 true
var y = 100 / "1000";
isNaN(y); // 返回 false

try it"

Division by zero is infinity, infinity is a number:

## Examples

var x = 1000 / 0;
isNaN(x); // 返回 false

try it"

## 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 x = 123;
var y = new Number(123);
typeof(x) // 返回 Number
typeof(y) // 返回 Object

try it"

## Examples

var x = 123;
var y = new Number(123);
(x === y) // 为 false，因为 x 是一个数字，y 是一个对象

try it"

## Digital Properties

• MAX_VALUE
• MIN_VALUE
• NEGATIVE_INFINITY
• POSITIVE_INFINITY
• NaN
• prototype
• constructor

## Numerical Methods

• toExponential ()
• toFixed ()
• toPrecision ()
• toString ()
• valueOf ()