There is no just "decimal"; you need to assume one of the concrete numeric type (such as int, unsigned int, double, etc.) and determine if the string matches it.
Answering this question by just direct analysis of the string is not easy at all. For example, if you checkup that a string is composed of digits only, it will not guarantee that the string can be successfully parsed to any available numeric type.
The idea is to actually try the parsing to a numeric type and than see if it was successful or not.
Unfortunately, the methods which are usually used for the purpose of "conversion" are not good enough. Be careful. The function
atoi
,
atol
,
atof
,
strtol
all return zero if no valid conversion could be performed. What a shame! For example,
atoi
will return the same value of zero if the input string is "000", or "gibberish". No good. See
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/[
^].
To solve the problem, use
sscanf
instead, as this function returns the number of successfully numeric parameters successfully parsed from the string based on assumed formats. See
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/[
^].
There are other methods suck as using
std
streams and reading value of the chosen type from a stream, and also using boost
lexical_cast
. See this discussion:
http://stackoverflow.com/questions/1012571/stdstring-to-float-or-double[
^].
—SA