The attributes and methods of the number object of JavaScript native object _javascript tips

Source: Internet
Author: User
Tags arithmetic numeric square root

To create the syntax for a number object:

Copy Code code as follows:

var myNum = new number (value);
var myNum = number (value);

When number () and operator new are used together as constructors, it returns a newly created number object. If you do not use the new operator to call number () as a function, it converts its arguments to an original numeric value and returns this (NaN) if the conversion fails.

Max_value

The Max_value property is the largest number that can be represented in JavaScript. Its approximate value is 1.7976931348623157 x 10308. The biggest negative number is-max_value.

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

Copy Code code as follows:

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

Min_value

The Min_value property is the smallest number that can be represented in JavaScript (close to 0, but not negative). Its approximate value is 5 x-324 .

All numbers smaller than min_value are 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 shows that some arithmetic operations (such as the square root of a negative number) do not result in numbers. Method parseint () and parsefloat () return this value when the specified string cannot be resolved. For a function that returns a valid number in a regular scenario, you can also use this method to illustrate its error condition with Number.NaN.

JavaScript outputs Number.NaN as NaN. Note that the result of NaN being compared 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 isNaN ().

Note: Global variables nan and Number.NaN are the same, and Nan is an not-configurable, modifiable property.

Copy Code code 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 property represents a value less than-number.max_value. The value represents a negative infinity.

JavaScript uses-infinity when it displays negative_infinity. The arithmetic behavior of this value is very similar to infinity. For example, any multiplier infinite result is still infinite, and any number that is removed by infinity is 0.

-infinity and number.negative_infinity are equal.

Copy Code code 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 property represents a value greater than number.max_value. The value represents a positive infinity.

JavaScript uses INFINITY when it displays positive_infinity. The arithmetic behavior of this value is very similar to infinity. For example, any multiplier infinite result is still infinite, and any number that is removed by infinity is 0.

Infinity and number.positive_infinity are equal.

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

Copy Code code 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 to a string and returns the result.

Numberobject.tostring (Radix)

The parameter radix is optional. A rule that represents the cardinality of a number, making an integer between 2 ~ 36. If this argument is omitted, base 10 is used, and it is recommended that you 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.

Throws a TypeError exception when the object that calls the method is not number.

Copy Code code 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 string in a local format.

A string representation of a number, as determined by the implementation, formatted according to the local specification, and may affect the punctuation marks used in decimal or thousand-bit delimiters.

Throws a TypeError exception when the object that calls the method is not number.

Copy Code code as follows:

var a = 123456;

Console.log (A.tolocalestring ()); 123,456
Console.log (a.tolocalestring ("Zh-hans-cn-u-nu-hanidec")); 123, 456

More parameters can be referred to: MDN

ToFixed ()

The ToFixed () method can be rounded to a number that specifies the number of decimal places.

Numberobject.tofixed (num)

Parameter num is required. Specify the number of digits, which are values between 0 ~ 20, including 0 and 20, and some implementations can support a larger range of values. If this argument is omitted, 0 is substituted.

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

Throws an exception rangeerror when NUM is too small or too large. The value between 0 ~ 20 does not throw the exception. Some implementations support a larger or smaller range of values. Throws a TypeError exception when the object that calls the method is not number.

Copy Code code 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 handling of floating point numbers, toFixed () method shows the result is not called "rounding" or "four homes six into 50% pairs", but four homes, six, five of the performance is very confusing.

Copy Code code as follows:

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

It is recommended to write your own method to replace the tofixed () default behavior, you can refer to: So on the discussion:

Copy Code code 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 the value of an object into an exponential counting method.

Numberobject.toexponential (num)

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

Returns the string representation of Numberobject, using the 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, replenished with 0 so that it reaches the specified length.

Throws an exception rangeerror when NUM is too small or too large. The value between 0 ~ 20 does not throw the exception. Some implementations support a larger or smaller range of values. Throws a TypeError exception when the object that calls the method is not number.

Copy Code code as follows:

var num = 10000.1234;

Console.log (Num.toexponential ()); 1.00001234e+4
Console.log (Num.toexponential (2)); 1.00e+4
Console.log (Num.toexponential (10)); 1.0000123400e+4

Toprecision ()

The Toprecision () method can format a numeric value as a string in decimal form.

Numberobject.toprecision (num)

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

Copy Code code 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.