How can I check if a string is numeric before actually converting it to a number? [modified]
-
In C#, the Parse method of int32, Double, single will raise an FormatExcepton if the input string is invalid (non-numeric) during conversion. Is there an easy way to check it before just calling the Parse method and then receiving such an exception? I don't wanna write a wrapper and catch such an exception which would be stupid for this kind of conversion. Thanks. -- modified at 23:18 Thursday 29th March, 2007
-
In C#, the Parse method of int32, Double, single will raise an FormatExcepton if the input string is invalid (non-numeric) during conversion. Is there an easy way to check it before just calling the Parse method and then receiving such an exception? I don't wanna write a wrapper and catch such an exception which would be stupid for this kind of conversion. Thanks. -- modified at 23:18 Thursday 29th March, 2007
int.TryParse double.TryParse
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
int.TryParse double.TryParse
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Thank you. But those TryParse methods are new in .Net 2; actually I need similar stuff in .Net 1.1 .
No, they aren't.
double.TryParse()
definitely is part of 1.1!Regards, mav -- Black holes are the places where God divided by 0...
-
No, they aren't.
double.TryParse()
definitely is part of 1.1!Regards, mav -- Black holes are the places where God divided by 0...
-
Just checked: double.TryParse() is available in 1.1, but there is no such version for int32 and Single. Thanks,
Hello, You could use double.TryParse with "NumberStyles.Integer" and then Convert the doubleValue to Integer.
double d;
int yourInt;
if(double.TryParse(YoureString,System.Globalization.NumberStyles.Integer, CultureInfo.CurrentCulture, out d))
{
yourInt = Convert.ToInt32(d);
}All the best, Martin
-
Hello, You could use double.TryParse with "NumberStyles.Integer" and then Convert the doubleValue to Integer.
double d;
int yourInt;
if(double.TryParse(YoureString,System.Globalization.NumberStyles.Integer, CultureInfo.CurrentCulture, out d))
{
yourInt = Convert.ToInt32(d);
}All the best, Martin