In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how javascript converts data to integers". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Javascript method of converting integers: 1. Use the parseInt () function with the syntax "parseInt (" value to be converted ")". 2. Use the bit operator, such as "~ ~ 123.45". 3. Use "Math.floor" and "Math.ceil" syntax.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Method 1:parseInt () function
ParseInt () is a global method that converts values to integers. The conversion process is as follows:
Parse the character at position 0 first, and return NaN directly if it is not a valid number.
If the character at position 0 is a number, or can be converted to a valid number, continue to parse the character at position 1, and if it is not a valid number, directly return the valid number at position 0.
And so on, analyze each character one by one from left to right until a non-numeric character is found.
ParseInt () converts all the legal numeric characters from the previous analysis to numeric values and returns them.
Console.log (parseInt ("123abc")); / / return numeric 123console.log (parseInt ("1.73")); / / return numeric 1console.log (parseInt (".123")); / / return value NaN
The point in the floating point is an illegal character for parseInt (), so the value of the decimal part is not converted.
If it is a numeric string that begins with 0, parseInt () treats it as an octal number: it is converted to an octal value and then returned as a decimal number.
If it is a numeric string that begins with 0x, parseInt () treats it as a hexadecimal number: it is converted to a hexadecimal value and then returned as a decimal number.
Var d = 010; / / octal number string var e = 0x10; / / hexadecimal number string console.log (parseInt (d)); / / return decimal number 8console.log (parseInt (e)); / / return decimal number 16
ParseInt () also supports base mode, which converts binary, octal, hexadecimal, and other numeric strings to integers. The base mode is specified by the second argument of the parseInt () function.
Example 1
The following code converts the hexadecimal number string "123abc" to a decimal integer.
Var a = "123abc"; console.log (parseInt (a meme16)); / / returns a decimal integer 1194684
Example 2
The following code converts binary, octal, and decimal number strings to decimal integers.
Console.log (parseInt ("10", 2)); / convert the binary number 10 to a decimal integer, to 2console.log (parseInt ("10", 8)); / / convert the octal number 10 to a decimal integer, to 8console.log ("10", 10); / / convert the decimal number 10 to a decimal integer, to 10
Example 3
If the first parameter is a decimal value with a 0 prefix, to avoid being misunderstood as an octal number, you should specify a second parameter value of 10, which displays the defined base mode instead of using the default base mode.
Console.log (parseInt (010)); / / convert the default base mode number 010 to decimal integer 10console.log (parseInt (0108)); / / convert octal number 010 to decimal integer 8console.log (parseInt (01010)); / / convert decimal number 010 to decimal integer 10
Method 2: bit operator
Console.log (0 | "123.45") / / 123console.log (0 | 123.45) / / 123console.log (0 ^ 123.45) / / 123console.log (~ 123.45) / / 1232
Principle: javascript does not have the concept of integers, all numerical types are double-precision floating-point numbers. When using a bit operator, it first converts the Operand into an integer to facilitate operation. While 0 is different from other values or bitwise or will not change the operation value.
Methods 3:Math.floor and Math.ceil
Example
Console.log (Math.floor (2.3)) / / 2console.log (Math.floor (- 2.3)) / /-3console.log (Math.ceil (2.3)) / / 3console.log (Math.ceil (- 2.3)) / /-2
The two are insufficient:
Math.floor gets the smallest integer of a number, while Math.ceil gets the largest integer. So if we rounded the original minus 2.3, we would get minus 2, but with Math.floor we would get minus 3. 2.3 what you get with Math.ceil is 3, but what we want is 2.
Resolve:
/ / define a function function getInt (val) {return val > 0? Math.floor (val): Math.ceil (val);} "how javascript converts data to integers" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 0
*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.