In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today Xiaobian to share with you about JavaScript string to digital code how to write the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.
ParseInt
According to the JsPerf.com benchmark, most browsers respond best to parseInt. Although it is the fastest way, there are some common pitfalls when using preseInt:
ParseInt ('08') / / returns 0 part of the old browser. ParseInt (' 44.jpg') / / returns 44
ParseInt: when no cardinality is passed in, the default cardinality is 10 parseInt (num, 10). If you don't know the type of num attribute, do not use parseInt for string conversion.
ParseFloat
If you don't analyze hexadecimal numbers, this is a very good choice. For example:
ParseInt (- 0xFF) / / returns-255parseInt ("- 0xFF") / / returns-255parseFloat (- 0xFF) / / returns-255parseFloat ("- 0xFF") / / returns 0
Note: the negative hexadecimal number in the string is a special case. If you parse it with parseFloat, the result is incorrect. In order to avoid NaN in the program, you should check the converted value.
ParseFloat ('44.jpg') / / return 44
ParseFloat: be careful when converting hexadecimal numbers. If you don't know the type of object you want to convert, don't use parseFloat.
Bitwise non-
You can convert a string to an integer, but it is not a floating point number. If it is a string conversion, it returns 0
~ ~ 1.23 / / returns 1 ~ "1.23" / / returns 1 ~ "23" / / returns 23 ~ "Hello world" / / returns 0
What is the principle of this? By flipping) each bit, also known as the A1 complement of a number. You can use it, but note that it can only be used to store integers. So usually don't use it unless you can be sure that the number is between 32-bit integers (because of the specification of the calling ToInt32).
Bitwise not: use it to ensure that there are no characters in the input, only for integers.
Number
Number has the same problem with the transformation mentioned above, trying to find out the number you gave him during parsing:
Number / / returns 23Number / / returns 19
Note: 023 is actually an octal number, no matter what you do, it returns 19; it's the same for hexadecimal numbers without single or double quotes.
Number is also one of the slowest in JsPerf.
Number: hardly ever use it.
Unary cloud operator
"1.23" * 1 / / returns 1.23 "0xFF"-0 / / returns 255 "0xFF.jpg" / 1 / / returns NaN+ "023" / / returns 23
The unary operator is different from other parsing methods, and if it is a nan value, it also returns NaN. This is my favorite method of numerical conversion, because I don't think any object with characters should be regarded as zero or "guess" based on how many bits it has. I basically use the + operator because this approach is not easily confused. Although the use of-0 is also good, it does not well express the original meaning of converting to numbers.
These are all the contents of this article entitled "how to write the code for converting JavaScript strings to numbers". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 265
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.