Click here to Skip to main content
15,902,926 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to know how to identify a whole string if it is a Number, instead of each digit (using the index).

Example:

string number = "-1.234";

char.IsNumber( number, 0 ); //Output = FALSE

Because the - is not a number, but if I check it as a whole number then it will be TRUE...

Any other way to do not use the index?
Posted

Could you use Decimal.TryParse?
 
Share this answer
 
Comments
jalmonte 8-Oct-10 11:14am    
Like, double.TryParse(number, but what I put here if just want to see if is True or False )
Marc A. Brown 8-Oct-10 11:17am    
You mean for the second (out) parameter? Create a variable of type double and pass it. If you don't need to know what the value is, just ignore the value of that variable after the call to TryParse. Make sense?
I think, you want like this
string number = "-1.234";
            double test = 0;

            if (double.TryParse(number,out test) && test >= 0)
                MessageBox.Show("Yes");
            else
                MessageBox.Show("No");
 
Share this answer
 
Comments
Dalek Dave 8-Oct-10 16:15pm    
Good Answer
shakil0304003 9-Oct-10 2:59am    
Thanks
Look at the Int32.TryParse Method[^] and Double.TryParse Method[^]: they returns true if the string content represents an int or a double respectively, and false otherwise.
 
Share this answer
 
You can also write an extension method that uses TryParse.
 
Share this answer
 
You can use a regular expression
 
Share this answer
 

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