CString转换成float后并没有内置的函数判断是否转换成功,如果没转换成功,没有提示,就进行下一步运算,肯定有错误!!!尤其是要把输入的数据写入到数据库中,没有判断就坑了。。。
因此小马写一个判断转换成浮点数是否成功的函数:
以下程序基于MFC:Dlg_Update_Id_Number 是我定义的类名:
bool Dlg_Update_Id_Number::isNumber(CString Input)
{
bool Output=true;
for(int i=0;i<Input.GetLength();i++)
{
if(Input.GetAt(i)=='.' && Input.GetLength()!=1)continue;
if(Input.GetAt(i)>47 && Input.GetAt(i)<58)continue;
else{
Output=false;break;
}
}
return Output;
}
在需要的地方判断:
CString temp;
if(isNumber(m_CString)){
temp.Format(_T("输入为:%s ,是实数"),m_CString);
MessageBox(temp);
}
else{
temp.Format(_T("输入为:%s ,不是实数"),m_CString);
MessageBox(temp);
}
看看结果: