I'm looking fo a function that will act like IsNumeric at VB
meaning: Returns True if an expression can be evaluated as a number
Printable View
I'm looking fo a function that will act like IsNumeric at VB
meaning: Returns True if an expression can be evaluated as a number
Short answer: No there isn't, but you could write your own (assuming you are talking about strings)
Long answer: Why do you need a function like that ? Many times in VB programmers tend to use strings for a host of different things, since it's a pain to define more appropriate data structures or I/O routines. I'd say that you probably don't need a function like this if you think about what your code should do and how it should be designed from a C++ point of view.
well actually I do need this function
I'm getting a string from a server and need to know if it's numeric...
How involved does the function have to be ? VBs IsNumeric is actually a bit more complex than it first appears, since it will recognize number formats dependent on the current thread locale. For example on an english system, IsNumeric("3,182,263.4746") will return True. If you need that kind of handling then have a look at GetNumberFormat in MSDN and work from there.
If your numbers are only composed of decimal digits (no decimal dot, no comma, no hexadecimal numbers, no Unicode numbers like arabic etc.), then it's much easier.
what about this?
Code:bool is_numeric( const std::string& s )
{
std::stringstream ss(s);
double d;
ss >> d;
return ss.good();
}
Look at strtod, strtol and strtoul. If you use the endptr parameter then you might have a very simple way to do what you need to do.
mwilliamson
what is 'good' function?
is that enough or that there are more options for numeric value?
I don't have arabic numbers
bool IsNumeric(char ch)
{
if(ch >= '0' && ch <= '9')
return true;
return false;
}
'good()' return true if the stream is okay (the goodbit is 'set')...Quote:
Originally posted by avi123
what is 'good' function?
Well...that should be fine...Quote:
Originally posted by avi123
is that enough or that there are more options for numeric value?
I don't have arabic numbers