December 2007
Intermediate to advanced
896 pages
19h 57m
English
You have a string that possibly contains a numeric value. You need to know whether this string contains a valid number.
Use the static TryParse method of any of the numeric types. For example, to determine whether a string contains a double, use the following method:
string str = "12.5";
double result = 0;
if(double.TryParse(str,
System.Globalization.NumberStyles.Float,
System.Globalization.NumberFormatInfo.CurrentInfo,
out result))
{
// Is a double!
}This recipe shows how to determine whether a string contains only a numeric value. The TryParse method returns true if the string contains a valid number without the exception that you will get if you use the Parse method.
The "Parse" and "TryParse" topics in the MSDN documentation.
Read now
Unlock full access