JavaScript native Object properties and methods detailed--number object

Source: Internet
Author: User

To create the syntax for the number object:

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 you do not use the new operator to call number () as a function, it will convert its arguments to an original value, and return this value (NaN if the conversion fails).

Max_value

The Max_value property is the largest number that can be represented in JavaScript. It has an approximate value of 1.7976931348623157 x 10308. The largest negative number is-max_value.

The larger number is infinity than the max_value. Max_value is a static property, so the calling method should be number.max_value.

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

Min_value

The Min_value property is the smallest number that can be represented in JavaScript ( nearly 0, but not negative ). It has an approximate value of 5 x 10-324.

All numbers smaller than Min_value will be converted to 0.

Min_value is a static property, so the calling method should be number.min_value.

NaN

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.

Number.NaN is a special value that indicates that some arithmetic operations, such as the square root of a negative number, are not numbers. Methods parseint () and parsefloat () return this value when the specified string cannot be resolved. For some functions that return a valid number in general, you can also use this method to illustrate the error condition with Number.NaN.

JavaScript outputs Number.NaN in the form of NaN. Note that theresult of NaN's comparison with other values is always unequal, including itself . Therefore, you cannot compare with Number.NaN to detect whether a value is a number, but only call IsNaN ().

Note: The global variables nan and Number.NaN are the same, and Nan is a non-configurable, non-modifiable property.

Console.log (parseint ("abc"));    //NaNconsole.log (nan = = = Nan);        /       ///////True

negative_infinity

The Negative_infinity property represents a value that is less than-number.max_value. This value represents negative infinity.

JavaScript uses-infinity when displaying negative_infinity. The arithmetic behavior of this value is very similar to infinity. For example, any multiply infinity result is still infinite, and any number is infinitely large except for the result of 0.

-infinity and number.negative_infinity are equal.

var x = (-number.max_value) * 2;  var y = number.negative_infinity;console.log (x);     /       /////True

positive_infinity

The Positive_infinity property represents a value greater than number.max_value. The value represents positive infinity.

JavaScript uses INFINITY when displaying positive_infinity. The arithmetic behavior of this value is very similar to infinity. For example, any multiply infinity result is still infinite, and any number is infinitely large except for the result of 0.

Infinity and number.positive_infinity are equal.

The isfinite () method can determine whether a parameter is a finite number .

var x = number.negative_infinity; var y = Number.positive_infinity;var z = Infinity; var a = "abc" ;;console.log (Isfinite (x)); //falseconsole.log (Isfinite (y)); //falseconsole.log (Isfinite (z)); //falseconsole.log (Isfinite (a)); //falseconsole.log (Isfinite (b)); //true        

toString ()

The ToString () method converts a Number object to a string and returns the result.

Numberobject.tostring (Radix)

The parameter radix is optional. An integer that specifies the cardinality of the number that makes 2 ~ 36. If this argument is omitted, cardinality 10 is used, and it is recommended to always take this parameter to prevent misunderstandings. For example, when Radix is 2 o'clock, Numberobject is converted to a string represented by a binary value.

The TypeError exception is thrown when the object calling the method is not number.

var a =N; console.log (a.tostring ());    //100console.log (a.tostring (Ten));  /        ////////

tolocalestring ()

The toLocaleString () method converts a number object to a locally formatted string.

The string representation of a number, as determined by the implementation, is formatted according to the local specification and may affect the punctuation used by the decimal or thousand separator.

The TypeError exception is thrown when the object calling the method is not number.

var a = 123456; Console.log (a.tolocalestring ());  //123,456console.log (a.tolocalestring ("Zh-hans-cn-u-nu-hanidec"));  // 123, 456    

More parameters can be found in: MDN

toFixed ()

The ToFixed () method rounds numbers to a number that specifies the number of decimal digits.

Numberobject.tofixed (num)

The parameter num is required . Specifies the number of decimal places, which are values between 0 and 20, including 0 and 20, and some implementations can support a larger range of values. If this argument is omitted, 0 will be used instead.

Returns a string representation of numberobject, with no exponential notation, and a fixed num digit number after the decimal point. If necessary, the number is rounded, or it can be topped with 0 so that it reaches the specified length. If NUM is greater than le+21, the method only calls Numberobject.tostring () and returns the string represented by exponential notation.

Throws an exception rangeerror when NUM is too small or too large. A value between 0 and 20 does not throw the exception. Some implementations support values in a larger range or a smaller range. The TypeError exception is thrown when the object calling the method is not number.

var n = 12345.6789; Console.log (n.tofixed ());            //12346console.log (n.tofixed (2));           /        ////////0.00

Note: Due to the processing of floating-point numbers, the result of the toFixed () method is not so-called "rounding" or "four six into 50% pairs", but four, six in, five of the performance is very chaotic.

////  0.04  

It is recommended that you write your own method to replace the tofixed () default behavior, as discussed on: so:

function(len) {    var temp = Math.pow (ten, Len);    var s = Math.ceil (this*return s/temp;  //0.05        

toexponential ()

The Toexponential () method converts the value of an object to an exponential count method.

Numberobject.toexponential (num)

The parameter num is optional. Specifies the number of decimal digits in the exponential notation, which is a value between 0 and 20, including 0 and 20, and some implementations can support a larger range of values. If this argument is omitted, as many numbers as possible are used.

Returns a string representation of numberobject, using exponential notation, which has a digit before the decimal point and a num digit after the decimal point. The decimal portion of the number is rounded and, if necessary, topped with 0 so that it reaches the specified length.

Throws an exception rangeerror when NUM is too small or too large. A value between 0 and 20 does not throw the exception. Some implementations support values in a larger range or a smaller range. The TypeError exception is thrown when the object calling the method is not number.

var num = 10000.1234; Console.log (Num.toexponential ());    //1.00001234e+4console.log (num.toexponential (2));   /      ///1.0000123400e+4

toprecision ()

The Toprecision () method formats a numeric value as a string in the form of a decimal number.

Numberobject.toprecision (num)

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

var num = 10000.1234; Console.log (Num.toprecision ());    //10000.1234console.log (Num.toprecision (2));   /      ///10000.12340

JavaScript native Object properties and methods detailed--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.