JavaScript Object -- Number

Source: Internet
Author: User
Number object is the packaging object of the original value. Syntax for creating a Number object: varmyNumnewNumber (value); varmyNumNumber (value); parameter value is the value of the Number object to be created, or the value to be converted to a Number... SyntaxHighlig

Number object

The Number object is the packaging object of the original value.

Syntax for creating a Number object:

Var myNum = new Number (value );

Var myNum = Number (value );

Parameters

The parameter value is the value of the Number object to be created, or the value to be converted to a Number.

Return Value

When Number () and operator new are used as constructors, it returns a newly created Number object. If the new operator is not used and Number () is called as a function, it will convert its own parameter into an original value and return this value (if the conversion fails, returns NaN ).

Number Object Attributes

FF: Firefox, IE: Internet Explorer

Attribute
Description
FF
IE
 
Constructor
Returns a reference to the Number function that creates this object.
1.0
4.0
 
MAX_VALUE
The maximum number that can be expressed.
1.0
4.0
 
MIN_VALUE
The smallest number that can be expressed.
1.0
4.0
 
NaN
Non-numeric value.
1.0
4.0
 
NEGATIVE_INFINITY
Negative infinity. This value is returned when overflow occurs.
1.0
4.0
 
POSITIVE_INFINITY
Positive infinity. This value is returned when overflow occurs.
1.0
4.0
 
Prototype
Allows you to add attributes and methods to objects.
1.0
4.0
 

Number object Method

FF: Firefox, IE: Internet Explorer

Method
Description
FF
IE
 
ToString
Converts a number to a string and uses the specified base number.
1.0
4.0
 
ToLocaleString
Converts a number to a string in the format of a local number.
1.0
4.0
 
ToFixed
Converts a number to a string. The result is a number of the specified digits after the decimal point.
1.0
5.5
 
ToExponential
Converts an object value to an exponential notation.
1.0
5.5
 
ToPrecision
Format the number to the specified length.
1.0
5.5
 
ValueOf
Returns the basic numeric value of A Number object.
1.0
4.0
 

Number Object Description

In JavaScript, numbers are a basic data type. JavaScript also supports the Number object, which is the packaging object of the original value. When necessary, JavaScript will automatically convert between the original data and the object. In JavaScript 1.1, you can use the constructor Number () to explicitly create a Number object, although this is not necessary.

The constructor Number () can be used directly as a conversion function instead of the new operator. When you call Number () in this way, it will convert its parameter into a Number, and then return the converted original value (or NaN ).

Constructors are usually used as placeholders for five useful numeric constants, these five useful numeric constants are the maximum number that can be expressed, the minimum number that can be expressed, positive infinity, negative infinity, and special NaN values. Note that these values are the attributes of the constructor Number (), rather than the attributes of a single Number object.

For example, the MAX_VALUE attribute is correct:

Var big = Number. MAX_VALUE

However, this is incorrect:

Var n = new Number (2 );

Var big = n. MAX_VALUE

For comparison, let's take a look at other methods of toString () and Number objects. They are the methods of each Number object, not the methods of Number () constructor. As mentioned above, when necessary, JavaScript will automatically convert the original value into a Number object, and call the Number method can be either a Number object or a raw Number value.

Var n = 123;

Var binary_value = n. toString (2 );

 

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.