Common confusing points for Javascript number types

Source: Internet
Author: User

1:nan (not a number)

Represents a case where the operand that would have returned a numeric value did not return a value. In ECMAScript, any number divided by 0 will return Nan "PS: actually only 0/0 will return Nan", and the positive (negative) number divided by 0 will return Infinity (-infinity).

Nan itself has two unusual features,1. Any operation involving Nan (e.g. NAN/10) will return nan,2, Nan, and any value that does not want to include the Nan itself.

1 console.log (nan = = nan); 2 console.log (nan = = = nan); 3 #results 4 // false 5 // false

2. IsNaN () function

ECMAScript defines the isNaN () function, which takes a parameter that can be any type that is used to determine if the parameter passed in is not a numeric value.

1 Console.log (IsNaN (NaN));2Console.log (IsNaN ("10"));3Console.log (IsNaN (20));4Console.log (IsNaN ("Blue"));5Console.log (IsNaN (true));6 #results7 //true8 //false9 //falseTen //true One //false

3. Numerical conversion

There are 3 functions that convert non-numeric values to numeric values: Number (), parseint (), parsefloat ().

Number ():

The transformation function number () can be used for any data type .

Number () function conversion rule:

A. If it is a numeric value, it is simply transferred and returned;

B. If it is a Boolean type, True and false return 1 and 0 respectively;

C. Returns 0 if it is a null value;

D. If it is undefined, return nan;

E. If it is a string, follow these rules:

If the string contains only numbers, it is converted directly to decimal values. (if 0 before the number is ignored, such as "0123" is converted to 123).

If the string contains a valid floating-point number form, it is converted to the corresponding floating-point value (leading 0 is still ignored).

If the string contains a valid hexadecimal format, it is converted to a decimal integer value of the same size.

If the string is empty (that is, it does not contain any characters), it is converted to 0.

If the string is contained in a format other than the one above, it is converted to Nan.

1 console.log (number ("0x10")); 2 console.log (number ("10ABC")); 3 console.log (Number ("")); 4 #results 5 //  - 6 // NaN 7 // 0

F. If it is an object, call the object's ValueOf () method first, then return with the preceding rule, and if the result is Nan, then call the object's ToString () method, and then convert the returned string value by the previous rule.

The other two functions are specifically used to convert a string to a numeric value , and these 3 functions may have different return results for the same input.

parseint ():

The parseint () function in the conversion string is, more to see whether it conforms to the digital pattern. It ignores whitespace characters in front of the string until the first non-whitespace character is found, and if the first character is not a number or minus sign, parseint () returns Nan, that is, parseint () converts the white space character back to Nan (different from numbers). Parsefloat () also returns Nan. If the first is a numeric character, parseint () continues parsing until the last character or a non-character is encountered.

1 console.log (parseint ("10ABC")); 2 Console.log (parseint ("")); 3 Console.log (parseint ("0xA")); 4 #results 5 // Ten 6 // NaN 7 // Ten

Note: ECMAScript 5 JavaScript engine, parseint () does not have the ability to parse octal, parseint (' 070 ') output 70! If you need to have the parseint () function convert the passed-in string to the specified number of bytes, you can pass it the second parameter: the technique used in the conversion (that is, how many binary). Not specifying cardinality means letting parseint () decide for itself how to parse a string, so in order to avoid error parsing, it is recommended to explicitly specify the cardinality regardless of the circumstances.

1 console.log (parseint ("AF", +)); 2 Console.log (parseint ("ten")); 3 Console.log (parseint ("ten")); 4 #results 5 // 175 6 // Ten 7 // 8

The parsefloat () function is similar to the parseint () function, parsing from the first character until the end of the string, or until the first invalid floating-point number character is encountered, parsing the first decimal point, and the second decimal point is not valid. The second difference between the parsefloat () function and the parseint () function, except for the decimal point, is that it always ignores the leading 0, which means that parsefloat () returns 0 when parsing the hexadecimal number.

1 console.log (parsefloat ("22.23.2")); 2 Console.log (parsefloat ("0x10")); 3 #results 4 // 22.23 5 // 0

Common confusing points for Javascript number types

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.