字符串化为整数

在这里插入图片描述

/**
  * 
  * @param str string字符串 
  * @return int整型
  */
function atoi( str ) {
    
    
    // write code here
    if(!str) return 0;
    str = str.trim()
    if(str.length){
    
    
        var res = parseInt(str)
        const min = Math.pow(-2,31)
        const max = Math.pow(2,31)-1
        if(res<min){
    
    
            return min
        }
        if(res>max){
    
    
            return max
        }
    }
    return res
}
module.exports = {
    
    
    atoi : atoi
};

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45284354/article/details/113826604