判断传入的字符串是否是数字 - IsTheNumber

2017-03-04 08:13:35  访问(1532) 赞(0) 踩(0)

  • // 字符是否是数字
    function IsDigit(cCheck) {
        // 判断传入的字符是否是数字
        return (('0' <= cCheck) && (cCheck <= '9'));
    }
    
    function IsTheNumber(str) {
    
        // 判断传入的字符串是否是数字
        
        if (str == null)
            return false;
    
        str = str + "";
        str = str.trim();
    
        var theLen = str.length;
        
        if (theLen == 0) 
        {
            return false;
        }
        
        
    
        var i = 0;
        var bFind = false;
    
        var chCurrent = str.charAt(i);
    
        if (chCurrent == '-' || chCurrent == '+') 
        {
            ++i;
            if(i == theLen)
                return false;
                
            chCurrent = str.charAt(i);
            
            if (!IsDigit(chCurrent))
                return false;
        }
        else if (!IsDigit(chCurrent))
            return false;
    
        ++i;
    
        for (; i < theLen; ++i) 
        {
            chCurrent = str.charAt(i);
            
            if (!IsDigit(chCurrent)) 
            {
                if (chCurrent == '.') 
                {
                    if (bFind)
                        return false;
    
                    bFind = true;
                }
                else
                    return false;
            }
        }
        
        return true;
    }
    

  • 输入1:
    输出:

标签:判断传入的字符串是否是数字 - IsTheNumber 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)