Click here to Skip to main content
15,917,652 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I want to check if the string which user entered is decimal or not??

help...
Posted
Comments
Sergey Alexandrovich Kryukov 4-Sep-11 21:33pm    
Not quite clear what do you mean by decimal:
How about "999999999999999999999999999999999999999999999999999999999999999999999" (won't parse into any predefined type).
How about "123E-12"? This is floating-point decimal. Do you really need decimal or numeric (so hexadecimal will also parse)?
--SA

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
 
Share this answer
 
Comments
[no name] 4-Sep-11 23:28pm    
If i see some one vote down something in codeproject, i will vote it up. coz in codeproject most of experts are a piece of a big headed sh*t!
Philippe Mori 5-Sep-11 9:10am    
Are you talking for question or answer voting.

For question, low votes are usually the result of poorly asked question, duplicate questions, spam (publicity, help for writting viruses...), not using Google or reading the documentation for trivial question.

For answer, low votes are often the result of the OP not liking the answer that he receive... when he ask to do something that cannot be done and want to do it anyway...
You can use next code:
bool IsDigitString(char* pszYourString)
{
    for (int i = 0; i < strlen(pszYourString); i++)
        if (!isdigit(pszYourString[i]))
            return false;

    return true;
}

where isdigit - builtin function (http://msdn.microsoft.com/en-us/library/fcc4ksh8(v=vs.71).aspx[^])
or
bool IsDigitString(char* pszYourString)
{
    int nResult = atoi(pszYourString);
    if (nResult == 0)
        if (strlen(pszYourString) != 1 ||
            pszYourString[0] != '0')
            return false;

    return true;
}

where atoi - builtin function (http://msdn.microsoft.com/en-us/library/hc25t012(v=vs.71).aspx[^])
 
Share this answer
 
v2
Comments
Philippe Mori 4-Sep-11 21:17pm    
By decimal, I would think that the OP also want to accept decimal symbol.
Sergey Alexandrovich Kryukov 4-Sep-11 21:31pm    
OP's question is just not formulated correctly. Please see my solution though.
--SA
Sergey Alexandrovich Kryukov 4-Sep-11 21:30pm    
This question only looks so easy, but it is not so easy. It's easy in .NET, not C#. First solution can be considered formally correct, if OP means just this -- a string of decimal digits. The question is formulated not quite correctly, that's it. Most likely, the additional requirement is to check is the string can be parsed to some available numeric type. In this case, your first solution will give wrong answer if the string is too long.

Second solution fails to give correct answer is the string is input string is "000" or "-0" or "-000".

I just had to vote 3.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900