Summary of JS digital conversion research

Source: Internet
Author: User

There are three methods for converting JavaScript data to numbers:

• Forced conversion: all basic JS data types are converted to the corresponding type (Number (v ))
• Native functions, which have different results (parseInt (v, radix), parseFloat (v ))
• Implicit conversion: During execution, JS will convert the variable to the corresponding type (bit computing, mathematical computing)
The original values can be simply divided into three types: objects, special values, and strings.

The special values in JS include the following.

• Undefined, undefined. This is the value of an attribute that is declared but not assigned a value or does not exist in an object.
• NaN/Infinity, two stubborn "Numbers" (typeof = number), representing non-numbers and infinite numbers respectively. It seems useless. If the conversion to a number fails, the return value is NaN.
• Null, used as the zero value object (typeof = object ).
• True/false, Boolean value, indicating true or false, equivalent to 1/0.
Strings can also be subdivided Based on the content. Javascript numeric expressions can be divided into valid and invalid values. Valid numbers have several different classification methods:

• Symbol: positive, negative, and unsigned
• Hexadecimal: octal, decimal, and hexadecimal
• Scientific note
• Decimal, integer, and decimal abbreviation
Based on the above classification, I made a Demo to test the results of different methods and Numerical Conversion. The following is the conversion in chrome.



The result is only a bit of computation that can convert any value to an isFinite. The result of the mathematical computation is the same as that of the forced conversion.

The conversion of special values is related to the original values and the conversion methods. Failed to convert all parseInt/parseFloat values. NaN is returned.

• All NaN/undefined values are converted to NaN using other methods;
• Infinity is converted to NaN by parseInt, and the remaining method does not change its value;
• Null/false/true cannot be converted by parseInt/parseFloat, and NaN is returned.
The conversion analysis of strings mainly refers to the conversion method, but all methods do not support numbers represented by octal instead of corresponding decimal numbers.

• The logical computation converts a valid expression except the negative hexadecimal number and removes the decimal part. An invalid expression returns zero.
• Math calculation/Number is similar to logical calculation, but does not give up decimal places. An invalid expression except a Null String returns NaN.
• ParseInt only retains the integer part. For an invalid expression, take the valid integer (decimal or hexadecimal, excluding scientific Notation) before the string for conversion, if no, NaN is returned.
• ParseFloat is similar to parseInt. The difference is that the fractional part can be recognized and retained, and the hexadecimal number is not supported.

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.