时间:2021-07-01 10:21:17 帮助过:7人阅读
示例代码
function _parseInt(str, radix) {
let str_type = typeof str;
let res = 0;
if (str_type !== 'string' && str_type !== 'number') {
// 如果类型不是 string 或 number 类型返回NaN
return NaN
}
// 字符串处理
str = String(str).trim().split('.')[0]
let length = str.length;
if (!length) {
// 如果为空则返回 NaN
return NaN
}
if (!radix) {
// 如果 radix 为0 null undefined
// 则转化为 10
radix = 10;
}
if (typeof radix !== 'number' || radix < 2 || radix > 36) {
return NaN
}
for (let i = 0; i < length; i++) {
let arr = str.split('').reverse().join('');
res += Math.floor(arr[i]) * Math.pow(radix, i)
}
return res;
}相关推荐:
JS使用parseInt解析数字实现求和实例详解
js中parseInt函数的简单介绍
JavaScript探秘:用parseInt()进行数值转换
以上就是JS手写parseInt的实例的详细内容,更多请关注Gxl网其它相关文章!