Depending on the type of number it is, you could use
xxx.TryParse()
(where "xxx" is the numeric data type in question).
string x = "1234";
string y = "-1234";
string z = "12b34";
int value;
if (int.TryParse(x, out value))
{
}
if (int.TryParse(y, out value))
{
}
if (int.TryParse(z, out value))
{
}
else
{
}
When
TryParse()
returns,
value
contains the 32-bit signed integer value equivalent of the number contained in the source string, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the source string parameter is
null
or
String.Empty
, is not of the correct format, or represents a number less than
int.MinValue
or greater than
int.MaxValue
. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
All of the intrinsic numeric types have a
TryParse()
method.