JS digital conversion research summary

  • 2020-03-30 01:07:02
  • OfStack

There are three types of methods for JS to convert data into Numbers, including:

The & # 8226; Cast, the basic data types of JS are all converted in this way to the corresponding type (Number(v))
The & # 8226; Native function with different results (parseInt(v,radix), parseFloat(v))
The & # 8226; Implicit conversion. When executed, JS will convert the variable to the corresponding type (bit calculation, mathematical calculation).
The original values of the transform can also be simply divided into three categories: objects, special values, and strings.

Special values in JS include the following.

The & # 8226; Undefined, undefined, when a variable is declared but not assigned or a property that doesn't exist on an object is this value.
The & # 8226; NaN/Infinity, these are two stubborn "Numbers" (typeof == number) for non-numeric and infinite Numbers, respectively. It doesn't seem to work, but if the conversion fails, the return value is basically NaN.
The & # 8226; Typeof == object.
The & # 8226; True /false, Boolean, true or false, is the same thing as 1/0.
For strings, you can also subdivide them by content. According to JS numeric expression, can be divided into legal and illegal. Valid Numbers can be classified in several different ways:

The & # 8226; Signs: plus, minus, unsigned
The & # 8226; Base: octal, decimal, hexadecimal
The & # 8226; Scientific notation
The & # 8226; Decimal, integer, and short for decimal
According to the classification of the above, I made a link: (http://demo.jb51.net/js/2013/tonumber.html), is used to test the different methods, the results of numerical transformation. The screenshot below shows the conversion in chrome.

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/tonumber.png" >

See the result only bit calculation can convert any value to finite number (isFinite), mathematical calculation and cast the result is the same.

The conversion of a particular value is related to both the original value and the conversion method. ParseInt /parseFloat all conversions failed and NaN is returned.

The & # 8226; NaN/undefined is fully converted to NaN by other methods;
The & # 8226; Infinity is converted to NaN by parseInt, and the rest of the methods do not change its value;
The & # 8226; Null /false/true cannot be converted by parseInt/parseFloat and returns NaN.
The conversion analysis of the string mainly looks at the conversion method, but all methods do not support octal representation of the number, but as the corresponding decimal number.

The & # 8226; The logical computation transforms the legal expression except the negative hexadecimal number, and omits the fractional part; An invalid expression returns zero.
The & # 8226; Mathematical calculation /Number is similar to logical calculation, but does not omit decimals; An invalid expression other than an empty string returns NaN.
The & # 8226; ParseInt also retains only the integer part; However, for an illegal expression, take the string before the valid integer (decimal, hexadecimal, not including scientific notation) part of the conversion, no return NaN.
The & # 8226; ParseFloat is similar to parseInt except that it recognizes and retains decimal parts and does not support hexadecimal Numbers.

Related articles: