判断字符串是否为数字

来源:互联网 发布:阿里云 centos7 mysql 编辑:程序博客网 时间:2024/06/15 18:03
//判断字符窜是否为数字BOOL IsStringNum(const CString &strNum ){ CString strCheck = strNum; strCheck.TrimLeft(); strCheck.TrimRight(); if(strCheck.IsEmpty()) return FALSE; ///////////////////////////////////////////// char chCheck; for(int i = 0; i < strCheck.GetLength(); i++) {  chCheck = strCheck.GetAt(i);   if(!::IsCharAlphaNumeric(chCheck))     return FALSE;   if(::IsCharAlpha(chCheck))     return FALSE;  } return TRUE;}
 
BOOL IsStringFloat(const CString &strNum){ CString strCheck = strNum; strCheck.TrimLeft(); strCheck.TrimRight(); if(strCheck.GetLength() == 0) return FALSE; ///////////////////////////////////////////// if(strCheck.GetAt(0) == '.' || strCheck.GetAt(strCheck.GetLength() - 1) == '.')  return FALSE; ///////////////////////////////////////////// char chCheck; int  iDotCount    = 0; BOOL bCountDot    = FALSE; int  iAfterDotNum = 0; for(int iIndex = 0; iIndex < strCheck.GetLength(); iIndex++) {  chCheck = strCheck.GetAt(iIndex);  switch(chCheck)  {   case '0':   case '1':   case '2':   case '3':   case '4':   case '5':   case '6':   case '7':   case '8':   case '9':    {     if(bCountDot)     {      iAfterDotNum++;      if(iAfterDotNum > 2)       return FALSE;     }     break;    }         case '.':    {     iDotCount++;     bCountDot = TRUE;     if(iDotCount > 1) return FALSE;     break;    }   case '-':    {     if(iIndex != 0)      return FALSE;     break;    }   default:    {     return FALSE;    }  } } return TRUE;}
0 0
原创粉丝点击