javascript - Javascript 中的 parseInt() 是如何工作的?
问题描述
console.log(parseInt('01abbb')) // 1
console.log(parseInt('31xyz')) // 31
console.log(parseInt('zyz31')) // NaN
console.log(parseInt('31xyz1')) // 31
是否parseInt()
忽略字符恰好不是整数的索引中的后缀?
解决方案
从字符串转换为数字时,您只会得到数字直到有意义的地方。
console.log(parseInt('01abbb')) // 1 -> it is started by 01 before chars
console.log(parseInt('31xyz')) // 31 -> it is started by 31 before chars
console.log(parseInt('zyz31')) // NaN -> it is not started by numbers
console.log(parseInt('31xyz1')) // 31 -> it is started by 31 before chars
推荐阅读
- ruby-on-rails - Ruby on Rails 中的 IP 匿名化(使用 IP 代理)
- kubernetes - 将 OpenShift 机密安装到具有受限访问权限的卷/文件中
- python - 如何删除模型但将数据库表保留在 Django
- ionic-framework - 页面上的完整搜索包括 UI 中的隐藏文本
- docker - Kubernetes cronjob args with env vars
- angular - 什么是
{{"TABLE.TRANSACTION_NUMBER"| 翻译}}
在角度? - verilog - 无法解析网络“sda_reg”的多个常量驱动程序
- python - Python清除dict中的每个列表
- flutter - Flutter:将下载的图像传递到第二个屏幕
- angular - Rxjs Subject/Observable 订阅列表数组