newstandard
asked on
Test if a CString is Numeric
I want to create an IsNumeric(CString) function. I have some code that seems to work, but since I am new to c++ I want to make sure it is the best way to do this.
BOOL myClass::isNumeric(CString text)
{
CString temp;
temp.Format("%d", _ttoi(text));
if (temp.GetLength()==text.Ge tLength()) return TRUE;
else return FALSE;
}
BOOL myClass::isNumeric(CString
{
CString temp;
temp.Format("%d", _ttoi(text));
if (temp.GetLength()==text.Ge
else return FALSE;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#include <stdlib.h>
BOOL myClass::isNumeric(CString
{
long l;
char* pcCnvEnd;
l = strtol ( ( LPCTSTR) text, &pcCnvEnd, 10);
if ( *pcCnvEnd) { // conversion stopped at sth. that was not the trailing NULL byte
return FALSE;
}
return TRUE;
}