当前位置:网站首页>You don't know parseInt?
You don't know parseInt?
2022-04-21 10:32:00 【InfoQ】
Preface
parseIntparseInt(string, radix)radix2 ~ 36parseIntparseInt['1', '2', '3'].map(parseInt)parseIntparseIntSurprise one : parseInt (0.0000005) === 5 by true, Big doubts ?
parseInt50MDNparseIntparseIntstringradixstringtoStringparseInt(0.5).toString(); // '0.5'
(0.05).toString(); // '0.05'
(0.005).toString(); // '0.005'
(0.0005).toString(); // '0.0005'
(0.00005).toString(); // '0.00005'
(0.000005).toString(); // '0.000005'
(0.0000005).toString(); // '5e-7'
toString5e-7parseInt("0.0000005"); // 0
parseInt("0.000000005"); // 0
toStringparseInt(999999999999999999999) // 1
// (999999999999999999999).toString()
// '1e+21'
// 10000000000000000
parseInt(9999999999999999)
Surprise two :radix The default value is 10 Do you , In which case the return value is NaN?
2.1 radix The value is undefined、0 Or unspecified
radixundefined、0JavaScript- If you enter
stringWith0xor0Xstart , thatradixWill be assumed to be16, The rest of the string is parsed in hexadecimal .
- If you enter
stringWith0start ,ES5The decimal system is specified , But not all browsers support , Therefore useparseIntwhen , You need to specify theradix
- If you enter
stringStart with any other value ,radixThe value is10
googleparseInt("0x16"); // 22
parseInt("0888"); // 888
parseInt("123px"); // 123
2.2 radix Less than 2 or Greater than 36
radix2 ~ 36radix2360NaNparseInt("123px", 1); // NaN
parseInt("123px", 38); // NaN
parseInt("123px", -1); // NaN
2.3 In the string to be converted , All convertible numbers are not less than radix value
radix2'3456'0、1'3456'NaNparseInt("3456", 2); // NaN
parseInt("45px", 3); // NaN
2.4 summary
radixThe default value is10Do you ? answer :No,radixAnd think aboutstringParameters , IfstringWith0x(0X)start ,radixby16; If the0start , Different browsers may have different values ; Other casesradixby10
- In which case the return value is
NaN?
radixLess than2or Greater than36
- In the string to be converted , All convertible numbers are not less than
radixvalue ( for exampleparseInt('345', 2))
- The first non space character cannot be converted to a number ( for example
parseInt('a123'))
Surprise three :radix Parameter leads to the interview question
3.1 ['1', '2', '3'].map(parseInt)
// val Is an array value
// index Index the current array
// _arr For the current array
arr.map((val, index, _arr) => {});
['1', '2', '3'].map(parseInt)[parseInt("1", 0), parseInt("2", 1), parseInt("3", 2)];
parseInt("1", 0)accord with2.1,radixby0, AndstringIn characters1Start ,radixThe value is10, The value is1.
parseInt("2", 1)accord with2.2,radixLess than2, returnNaN
parseInt("3", 2)accord with2.3, In the string to be converted , All convertible numbers are greater thanradixvalue , returnNaN
[1, NaN, NaN]3.2 ['10', '10', '10', '10'].map(parseInt)
[parseInt("10", 0), parseInt("10", 1), parseInt("10", 2), parseInt("10", 3)];
'10'[10, NaN, 2, 3]Surprise four :parseInt Terminal segment of collateral branch
parseIntparseIntAllow leading and trailing spaces , Before conversion , The leading and trailing spaces of the string will be deleted first .
// Allow leading and trailing spaces
parseInt(" 123 "); // 123
// The space in the middle will be treated as a truncater
parseInt("123 456 "); // 123
parseIntYou can understand the sign
parseInt("+123"); // 123
parseInt("-123"); // -123
// You can only understand the first sign , The second non numeric character , Therefore return NaN
parseInt("+-123"); // NaN
- Use
parseInttransformationBigIntLoss of accuracy
parseIntBigIntNumber"n"let bigInt = 111111111111111111111111n;
parseInt(bigInt); // 1.1111111111111111e+23
let bigInt2 = 11n;
parseInt(bigInt2); // 11
Blog warehouse
Afterword
An early end to the epidemic Peace has returned to the world
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211030458606.html
边栏推荐
- Jeecgboot: the difference between online form control drop-down box and drop-down search box
- 塔米狗知识|上市公司收购的基本原则
- MKL and vs2019 configuration method
- The most easy to understand dependency injection and control inversion
- Zsh: segmentation fault solution
- Usage of SAP ABAP for all entries
- C#入门-利用正则表达式校验身份证号
- Nanny level tutorial on building personal home page (I)
- 使用对数器验证简单排序
- Moore thread cooperates with ampere computing
猜你喜欢
随机推荐
计量模型、实证stata代码合集,附顶刊示例
SQL:树形三层职业分类表的SQL文件
塔米狗企业并购平台上,法律服务商名单目录!
24 pictures to conquer border image
塔米狗知识|上市公司收购的基本原则
jeecgboot:online表单控件下拉框和下拉搜索框的区别
2022信息与未来预备刷题2《New Online Judge 1113: 位数问题》
Page navigation - declarative / programmatic navigation
SAP ABAP FOR ALL ENTRIES 的用法
一文读懂Seek Tiger推出创世节点的意义
postman设置环境变量,简单又实用
After reading this article, I'll teach you to play with the penetration test target vulnhub - dc9
管道通信
MySQL8.0学习记录07 - 数据类型之JSON
Summary of knapsack problem (0-1, complete, multiple knapsack problem)
My creation anniversary
C#入门-并行编程
【并发编程044】CAS循环时间太长,会有什么问题?
L1-045 宇宙无敌大招呼 (5 分)
Cisco Packet Tracer实验集合









