# If the string is all numbers

## Recommended Posts

Is there a standard way to tell if a string consists of all numbers or not?

##### Share on other sites
If you are using the .NET framework, there is a Char.IsDigit() method available. Otherwise you could convert characters in a string to their ASCII value and then check to see if the string contains the 0-9 number's ASCII values.

I am guessing there are more ways to do this.

##### Share on other sites
You might want to start here, isdigit, and explore the functions in the run time library until you find one that does what you want.

##### Share on other sites
Quote:
 Original post by TradoneIs there a standard way to tell if a string consists of all numbers or not?

What language are we talking about?

##### Share on other sites
Quote:
 Original post by TradoneIs there a standard way to tell if a string consists of all numbers or not?
If this is C++, a standard-ish method would be:
if (boost::algorithm::all(myString, boost::algorithm::is_digit()) {    // ...}
I don't have the reference in front of me so I might not have gotten the syntax exactly right, but it should be something close to that.

##### Share on other sites
This handles both Hex or Decimal numbers.

BOOL IsNumber(char *cString, BOOL bIsHex){    BOOL bIsDigit = TRUE;    while((*cString != 0) && bIsDigit)    {        if (bIsHex)        {            bIsDigit = isxdigit(*cString);        }        else        {                 bIsDigit = isdigit(*cString);        }        cString++;    }    return bIsDigit;}

(my rating will now go down for suggesting a "C" solution and not a c++ solution)

[Edited by - BeerNutts on April 24, 2006 3:02:13 PM]

##### Share on other sites
Quote:
 Original post by BeerNuttsThis handles both Hex or Decimal numbers.*** Source Snippet Removed ***
if (boost::algorithm::all(myString, boost::algorithm::is_xdigit()) {    // ...}
:-)

##### Share on other sites
And if for some silly reason you don't like Boost :)

// We need this wrapper to provide typedefs used by not1.struct isDigit {  typedef char argument_type;  typedef bool result_type;  result_type operator() (argument_type x) {    return std::is_digit(x);  }};if (std::find_if(myString.begin(), myString.end(), std::not1(isDigit())) ==     myString.end()) {  // We didn't find a non-digit, so they are all digits.}

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by BeerNuttsThis handles both Hex or Decimal numbers.*** Source Snippet Removed ***
if (boost::algorithm::all(myString, boost::algorithm::is_xdigit()) {    // ...}
:-)

Just what I was looking.
Everybody thanks for the contributions!

##### Share on other sites
Also keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.

##### Share on other sites
Quote:
 Original post by Anonymous PosterAlso keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.

You have a point here. Of course, testing if a string represents any kind of number can be tricky ("+-+123.344e008" is a number too). If you want to be able to spot all the different kind of numbers, I'm afraid you'll have to use a regular expression.

Regards,

##### Share on other sites
bool isStringNumber(char *str, int len){
bool returnResult = TRUE;

if((str[0] > '0') && (str[0] < '9')){
if(len > 0)
returnResult = isStringNumber(&(char[1]), len-1);
}else
returnResult = FALSE;

return returnResult;
}

##### Share on other sites
Quote:
 Original post by rdansiebool isStringNumber(char *str, int len){bool returnResult = TRUE;if((str[0] > '0') && (str[0] < '9')){ if(len > 0) returnResult = isStringNumber(&(char[1]), len-1);}else returnResult = FALSE;return returnResult;}
If you're specifically offering a C solution, then great, I suppose (although your function has a couple of errors in it and really doesn't need to be recursive).

However (and this is editorializing a bit) I don't quite understand why people insist on posting hand-rolled, untested, unsafe, and/or non-standard solutions to threads like this after the (arguably) correct solution has already been given.

Read the thread. The test can be performed in one line using boost, and if you don't like that Zahlman posted a nice alternative using functors and the standard library. If you're seriously proposing the above or something like it as a solution, I think you need to provide some supporting arguments as to why it's superior to what's already been suggested.

Anyway, I don't mean to be negative; really my motivation is just to clarify to those who might read this thread (and might not have the experience to sort the wheat from the chaff, as it were) that standardized solutions are generally to be preferred.

##### Share on other sites
Quote:
 Original post by jykHowever (and this is editorializing a bit) I don't quite understand why people insist on posting hand-rolled, untested, unsafe, and/or non-standard solutions to threads like this after the (arguably) correct solution has already been given.Read the thread. The test can be performed in one line using boost, and if you don't like that Zahlman posted a nice alternative using functors and the standard library.

Well, it *is* good to know the (written very specific to the problem) algorithms in case you ever find yourself in an environment (language, target device, etc. etc.) that doesn't provide support for what you want to do. But then, only really experienced programmers ought to be working in those environments - people who are capable of coming up with these things themselves, and getting them right (able to give a proof, and also have it pass tests) the first time (or at least within the first few times).

Sadly, the world doesn't always work that way ;(

##### Share on other sites
Quote:
Original post by Emmanuel Deloget
Quote:
 Original post by Anonymous PosterAlso keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.

You have a point here. Of course, testing if a string represents any kind of number can be tricky ("+-+123.344e008" is a number too).

That's technically an expression, not a numeric literal. (But the result of the expression is the literal "-123.344e008".)

##### Share on other sites
The topic is called "if the string is all numbers", so I assume that there are really only digits in the string, no sign, radix character or any other non-digit character.

But if they're allowed there is a really easy and lame way:
double strAsDouble = atof( theString );if( strAsDouble != 0.0 ) //a double can store a natural number without loss of precision, so comparison is possible

The only thing I'm wondering is that atof is returning 0.0, which can also be a valid result of converting a string to a number (according to http://www.cplusplus.com/ref/cstdlib/atof.html). That's really strange, that in the standard, there is a function with such errorneous error handling.

##### Share on other sites
What is the header file for boost::algorithm'?
Sorry, I know this is a dumb question, but I'm not getting any matches..

##### Share on other sites
Quote:
 Original post by TradoneWhat is the header file for boost::algorithm'?Sorry, I know this is a dumb question, but I'm not getting any matches..

You need Boost to use Boost::algorithm.

##### Share on other sites
Yea, I have the boost library installed.
I'm using their serialization class to serialize my map files, and also use their lexical cast :D

Do you know the header files that I need to include for boost::algorithms::all isdigit() ??

##### Share on other sites
boost/algorithm/string.hpp

:-)

yay! it works.
Thanks :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628391
• Total Posts
2982419

• 10
• 9
• 19
• 24
• 11