Home >Web Front-end >JS Tutorial >JS digital conversion research summary

JS digital conversion research summary

PHPz
PHPzOriginal
2016-05-16 17:06:591318browse

This article mainly introduces the implementation method of JS digital conversion. Friends in need can refer to it.

JS has three types and five methods of converting data to numbers, including:

• Forced conversion, the basic data types of JS are converted into the corresponding type (Number(v)) in this way
•Native functions, these two functions have different results (parseInt(v,radix), parseFloat(v))
•Implicit conversion, JS will convert the variable to the corresponding type (bit Calculation, mathematical calculation)
The converted original value can also be simply divided into three categories: objects, special values, and strings.

Special values ​​in JS include the following:

•undefined, undefined, this is the value of a property when a variable is declared but not assigned a value or an object does not exist.
•NaN/Infinity, these are two stubborn "numbers" (typeof == number), which represent non-numbers and infinite numbers respectively. It seems useless. If the conversion to a number fails, the return value is basically NaN.
•null, used as an object with zero value (typeof == object).
•true/false, Boolean value, represents true or false, equivalent to 1/0.

For strings, they can also be subdivided according to the content. According to JS numeric expressions, they can be divided into legal and illegal. There are several different classification methods for legal numbers:

•Signs: positive sign, negative sign, unsigned
•Base: octal, decimal, hexadecimal
•Scientific notation Methods
•Decimals, integers, and abbreviations for decimals

Based on the above classification, I made a Demo to test the results of different methods and numerical conversions. The screenshot below is the conversion under chrome.

JS digital conversion research summary

Look at the results. Only bit calculations can convert any value into a finite number (isFinite). The results of mathematical calculations and forced conversion are the same.

The conversion of special values ​​is related to the original value and the conversion method. All parseInt/parseFloat conversions fail and return NaN.

•NaN/undefined is fully converted to NaN using other methods;
•Infinity is converted to NaN by parseInt, and the remaining methods do not change its value;
•null/false/true cannot be parseInt/parseFloat conversion, returns NaN.

The conversion analysis of strings mainly depends on the conversion method, but all methods do not support numbers represented by octal, but are treated as corresponding decimal numbers.

•Logical calculation converts legal expressions except negative hexadecimal numbers and discards the decimal part; illegal expressions return zero.
• Mathematical calculation/Number is similar to logical calculation, but does not round off decimals; illegal expressions except empty strings return NaN.
•parseInt also only retains the integer part; but for illegal expressions, it takes the legal integer (decimal, hexadecimal, excluding scientific notation) part in front of the string and converts it, otherwise it returns NaN.
•parseFloat is similar to parseInt, except that it can recognize and retain the decimal part and does not support hexadecimal numbers.

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn