Link to home
Start Free TrialLog in
Avatar of newstandard
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.GetLength()) return TRUE;
      else return FALSE;
}
Avatar of jkr
jkr
Flag of Germany image

I'd use

#include <stdlib.h>

BOOL myClass::isNumeric(CString text)
{
   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;
}
ASKER CERTIFIED SOLUTION
Avatar of martynjpearson
martynjpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial