Explanation of the attributes and methods of the Number object of the JavaScript Native object _ javascript skills

Source: Internet
Author: User
This article describes the attributes and methods of the Number object of the JavaScript Native Object. This article describes the syntax, MAX_VALUE, MIN_VALUE, NaN, and other attributes or methods for creating the Number object, for more information, see Syntax for creating a Number object:

The code is as follows:


Var myNum = new Number (value );
Var myNum = Number (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 ).

MAX_VALUE

The MAX_VALUE attribute is the maximum number that can be expressed in JavaScript. The approximate value is 1.7976931348623157x10308. The maximum negative value is-MAX_VALUE.

The number that is larger than MAX_VALUE is Infinity. MAX_VALUE is a static attribute, so the call method should be Number. MAX_VALUE.

The code is as follows:


Console. log (Number. MAX_VALUE) // 1.7976931348623157e + 308

MIN_VALUE

The MIN_VALUE attribute is the smallest number that can be expressed in JavaScript (close to 0, but not a negative number ). The approximate value is 5x10-324.

All numbers smaller than MIN_VALUE are converted to 0.

MIN_VALUE is a static attribute, so the call method should be Number. MIN_VALUE.

NaN

The NaN attribute is a special value that represents a non-numeric value. This attribute indicates 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.

Number. NaN is a special value, indicating that the result of some arithmetic operations (such as finding the square root of a negative Number) is not a Number. The parseInt () and parseFloat () methods return this value when the specified string cannot be parsed. This method can also be used for functions that normally return valid numbers. Number. NaN is used to describe its error.

JavaScript outputs Number. NaN in the form of NaN. Note that the result of comparing NaN with other values is always not equal, including itself. Therefore, you cannot compare it with Number. NaN to check whether a value is a Number. Instead, you can only call isNaN () to compare it.

Note: The global variables NaN and Number. NaN are the same. NaN is an attribute that cannot be configured or modified.

The code is as follows:


Console. log (parseInt ("abc"); // NaN
Console. log (NaN = NaN); // false
Console. log (Number. NaN = NaN); // false
Console. log (isNaN (NaN); // true
Console. log (isNaN (Number. NaN); // true

NEGATIVE_INFINITY

The NEGATIVE_INFINITY attribute indicates a value smaller than-Number. MAX_VALUE. This value indicates negative infinity.

-Infinity is used when JavaScript displays NEGATIVE_INFINITY. The arithmetic behavior of this value is very similar to that of infinity. For example, the result of any multiplication of infinity is still infinite, and the result of division of any number by infinity is 0.

-Infinity and Number. NEGATIVE_INFINITY are equal.

The code is as follows:


Var x = (-Number. MAX_VALUE) * 2;
Var y = Number. NEGATIVE_INFINITY;
Console. log (x); //-Infinity
Console. log (y); //-Infinity
Console. log (x = y); // true

POSITIVE_INFINITY

The POSITIVE_INFINITY attribute indicates a value greater than Number. MAX_VALUE. This value indicates positive infinity.

When POSITIVE_INFINITY is displayed in JavaScript, Infinity is used. The arithmetic behavior of this value is very similar to that of infinity. For example, the result of any multiplication of infinity is still infinite, and the result of division of any number by infinity is 0.

Infinity and Number. POSITIVE_INFINITY are equal.

The isFinite () method can be used to determine whether a parameter is a finite number.

The code is as follows:


Var x = Number. NEGATIVE_INFINITY;
Var y = Number. POSITIVE_INFINITY;
Var z = Infinity;
Var a = "abc ";
Var B = 123;

Console. log (isFinite (x); // false
Console. log (isFinite (y); // false
Console. log (isFinite (z); // false
Console. log (isFinite (a); // false
Console. log (isFinite (B); // true

ToString ()

The toString () method converts a Number object into a string and returns the result.

NumberObject. toString (radix)

The radix parameter is optional. Indicates the base number of a number, so that 2 ~ An integer between 36. If this parameter is omitted, base 10 is used. we recommend that you always use this parameter to avoid misunderstanding. For example, when radix is 2, NumberObject is converted to a string represented by a binary value.

If the object that calls this method is not a Number, a TypeError error is thrown.

The code is as follows:


Var a = 100;

Console. log (a. toString (); // 100
Console. log (a. toString (10); // 100
Console. log (a. toString (2); // 1100100
Console. log (a. toString (8); // 144
Console. log (a. toString (16); // 64

ToLocaleString ()

The toLocaleString () method converts a Number object to a local string.

The string representation of a number is determined by the implementation. formatting based on local specifications may affect the punctuation marks used by decimal points or thousands of separators.

If the object that calls this method is not a Number, a TypeError error is thrown.

The code is as follows:


Var a = 123456;

Console. log (a. toLocaleString (); // 123,456
Console. log (a. toLocaleString ("zh-Hans-CN-u-nu-hanidec"); // One, two, three, four, five, six

For more parameters, see MDN.

ToFixed ()

The toFixed () method rounds a Number to a specified Number of decimal places.

NumberObject. toFixed (num)

The num parameter is required. Specifies the number of digits in a decimal place, ranging from 0 ~ Values between 20, including 0 and 20. some implementations support a larger value range. If this parameter is omitted, it is replaced by 0.

Returns the string representation of NumberObject. without the exponential notation, there is a fixed num digit after the decimal point. If necessary, the number is rounded off, or 0 can be used to make up for the specified length. If num is greater than le + 21, this method only calls NumberObject. toString () and returns the string represented by exponential notation.

When num is too small or too large, an exception RangeError is thrown. 0 ~ The value between 20 does not cause this exception. Some implementations support values in a larger or smaller range. If the object that calls this method is not a Number, a TypeError error is thrown.

The code is as follows:


Var n = 12345.6789;

Console. log (n. toFixed (); // 12346
Console. log (n. toFixed (2); // 12345.68
Console. log (n. toFixed (6); // 12345.678900
Console. log (1.23e + 20). toFixed (2); // 123000000000000000000.00
Console. log (1.23e-10). toFixed (2); // 0.00

Note: Due to the processing of floating point numbers, the result displayed by the toFixed () method is not "rounding" or "four homes, six homes, five homes, and five into double homes", but four homes and six homes, 5. The performance was messy.

The code is as follows:


// Chrome
Console. log (0.035). toFixed (2); // 0.04
Console. log (0.045). toFixed (2); // 0.04

We recommend that you write your own method to replace the default toFixed () behavior. for details, refer to the discussion on SO:

The code is as follows:


Number. prototype. toFixed = function (len ){
Var temp = Math. pow (10, len );
Var s = Math. ceil (this * temp)
Return s/temp;
}

Console. log (0.035). toFixed (2); // 0.04
Console. log (0.045). toFixed (2); // 0.05

ToExponential ()

The toExponential () method converts object values into exponential notation.

NumberObject. toExponential (num)

The num parameter is optional. Number of decimal places in the specified index counting method, ranging from 0 ~ Values between 20, including 0 and 20. some implementations support a larger value range. If this parameter is omitted, as many numbers as possible will be used.

Returns the string of NumberObject. it is expressed by exponential notation, that is, there is a digit before the decimal point, and there is a num digit after the decimal point. The decimal part of the number will be rounded off. if necessary, use 0 to fill it up so that it reaches the specified length.

When num is too small or too large, an exception RangeError is thrown. 0 ~ The value between 20 does not cause this exception. Some implementations support values in a larger or smaller range. If the object that calls this method is not a Number, a TypeError error is thrown.

The code is as follows:


Var num = 10000.1234;

Console. log (num. toExponential (); // 1.20.1234e + 4
Console. log (num. toExponential (2); // 1.00e + 4
Console. log (num. toExponential (10); // 1.20.123400e + 4

ToPrecision ()

The toPrecision () method can format a value into a string in decimal format.

NumberObject. toPrecision (num)

The num parameter is optional. Used to control the precision of numbers. This parameter is 1 ~ Values between 21 (including 1 and 21. If this parameter is omitted, the toString () method is called instead of converting the number to a decimal value.

The code is as follows:


Var num = 10000.1234;

Console. log (num. toPrecision (); // 10000.1234
Console. log (num. toPrecision (2); // 1.0e + 4
Console. log (num. toPrecision (10); // 10000.12340

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.