In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people don't understand the knowledge points of this article "how to use parseInt function", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how to use parseInt function".
parseInt()
The effect is to cast an argument (usually a string) to an integer.
It has two parameters, the second one can be omitted, let's show you common usage first
1. Conversion rule: turn the number part of the string from left to right into an integer, once the conversion fails, NaN is returned.
(1)Parameter is string type:
var a = parseInt ('10 '); //Convert string to integer type
console.log(a); // 10
var b = parrseInt ('10 true'); //convert significant digits to integers, extract leading digits
console.log(b); // 10
var c = parseInt ('10 true 20'); //convert only the first significant part to an integer
console.log(c); //10
var d = parseInt ('045 zoo'); //The first part is converted to an integer, and the 0 before the number is omitted
console.log(d); //45
var f = parseInt ('34.5 '); //integer part, decimal point non-numeric part, truncated
console.log(f); //34
var g = parseInt ('a3 '); //If it does not start with a number, turn it into a special value of NaN (Not a Number is not a number)
console.log(g); //NaN
(2)Parameters are numeric types:
var a = parseInt(3.4); //still converted to numbers, but rounded
console.log(a); //3
(3)Other data types: as long as the beginning does not contain significant digits, the result will be NaN
var a = parseInt(true);
console.log(a); //NaN
var b = parseInt(null);
console.log(b); //NaN
var c = parseInt(undefined);
console.log(c); //NaN
var d = parseInt([]);
console.log(d); //NaN
var e = parseInt({});
console.log(e); //NaN
Next, let's look at the usage of the second parameter
The second parameter: indicates the base, range 2--36 (used to explain the base rule of the first parameter)
var a = parseInt ('99 ', 10); //This parameter can be omitted if the string is decimal
console.log(a); // 99
var a = parseInt ('1001 ',2); //' 1001 'conforms to binary rules, converts to decimal integers
console.log(a); // 9
var a = parseInt ('1001 ',10); //' 1001 'also conforms to decimal, converted to decimal integer
console.log(a); // 1001
var b = parseInt ('234 ', 2); //' 234 'does not conform to binary rules
console.log(b); // NaN
var b = parseInt ('abcde ',2); //' abcde 'does not conform to binary rules
console.log(b); // NaN
var c = parseInt ('1022 ', 2); //The' 10 'part is binary, but the' 22 'part is not, extract the first valid part and convert it to decimal
console.log(c); //2
Some special notes
If it starts with 0x or 0X, it can be recognized even if the base rule is not stated.
var e = parrseInt ('0xa '); //0x is hexadecimal representation, a is 10 in hexadecimal, automatically switched to decimal after conversion to integer
console.log(e); //10
Try not to use the number where e appears, because only the beginning part can be identified, which will cause confusion in the result.
The parseFloat method should be used instead
parseInt("6.022e23", 10); //returns 6
parseInt(6.022e2, 10); //returns 602
Very large or very small numbers
parseInt(4.7 * 1e22, 10); //very large values become 4
parseInt(0.000000000434, 10); //very small values become 4
In the latest ES5 specification, numbers that begin with 0 are no longer recognized as octal.
parseInt("011"); //will be converted from decimal to 11
parseInt("011", 8) //octal specified, result 9
The above is the content of this article about "how to use parseInt function". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, 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: 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.