JavaScript Number Object

Source: Internet
Author: User

JavascriptNumber Object

JavaScript has only one numeric type.

You can use it or you can write a number without using a decimal point.

JavaScript numbers

JavaScript numbers can be used or written without using a decimal point:

Example Var pi=3.14; Use decimal point
var x=34; Do not use decimal points

The maximum or minimum number can be written by means of the scientific (exponential) notation:

Example Var y=123e5; 12300000
var z=123e-5; 0.00123

All JavaScript numbers are 64-bit

JavaScript is not a type language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integers, short, long, floating point, and so on.

In JavaScript, numbers are not classified as integer types and floating-point types, and all numbers are made of floating-point types. JavaScript uses the 64-bit floating-point format defined by the IEEE754 standard to represent numbers, which can represent a maximum value of ±1.7976931348623157 x 10308 and a minimum of ±5 x 10-324

Sign
value (aka Fraction/mantissa) Index
Bits (0-51) Bits (50-62) 1 bit (63)

Precision

Integers (not using decimal or exponential notation) are up to 15 bits.

The maximum number of decimal digits is 17, but the floating-point operation is not always 100% accurate:

instance var x = 0.2+0.1; Result would be 0.30000000000000004
Try it»

Octal and hexadecimal

If the prefix is 0, JavaScript interprets numeric constants as octal numbers, and if the prefixes are 0 and "X", they are interpreted as hexadecimal numbers.

instance var y = 0377;
var z = 0xFF;
Try it»

Never write 0 in front of the number unless you need to make an octal conversion.

By default, JavaScript numbers are displayed in decimal.

But you can use the ToString () method to output 16 binary, 8 binary, and 2 binary.

Example Var mynumber=128;
Mynumber.tostring (16); Returns 80
Mynumber.tostring (8); Returns 200
Mynumber.tostring (2); Returns 10000000
Try it»

Infinity (Infinity)

When the result of a numeric operation exceeds the upper limit (overflow) of the number that JavaScript can represent, the result is a special infinity (infinity) value, expressed as 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, represented by-infinity in JavaScript. The behavior characteristics of infinite values are consistent with what we expect: based on their addition, subtraction, multiplication, and divide operations, or infinity (and, of course, their positive and negative numbers).

Example mynumber=2;
while (mynumber!=infinity)
{
Mynumber=mynumber*mynumber; Calculate until Infinity
}
Try it»

Dividing by 0 also produces infinity:

instance var x = 2/0;
var y =-2/0;
Try it»

NaN-Non-numeric value

The NaN property is a special value that represents a non-numeric value. This property is used to indicate that a value is not a number. You can set the number object 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.

instance var x = +/"Apple";
IsNaN (x); Returns True
var y = 100/"1000";
IsNaN (y); Returns false
Try it»

Divide by 0 is infinity, Infinity is a number:

instance var x = 1000/0;
IsNaN (x); Returns false
Try it»

Numbers can be numbers or objects

Numbers can be initialized with private data, just like x = 123;

JavaScript numeric object initialization data, var y = new number (123);

instance var x = 123;
var y = new number (123);
typeof (X)//returns number
typeof (Y)//returns Object
Try it» example var x = 123;
var y = new number (123);
(x = = y)//is false because X is a number and Y are an object.
Try it»

Numeric properties
    • Max_value
    • Min_value
    • Negative_infinity
    • Positive_infinity
    • NaN
    • Prototype
    • Constructor
Digital method
      • Toexponential ()
      • ToFixed ()
      • Toprecision ()
      • ToString ()
      • ValueOf ()

JavaScript Number Object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.