First of all, this operation is called
parsing. This is what really happens when you try to interpret a string as a representation of a number.
This is how:
string input =
int value = int.Parse(input, System.Globalization.NumberStyles.AllowThousands);
or
string input =
int value;
bool success = int.TryParse(input, System.Globalization.NumberStyles.AllowThousands, null, out value);
Please see:
http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx[
^],
http://msdn.microsoft.com/en-us/library/zf50za27.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx[
^].
First variant may throw exception, so you may need to handle it and let a user a change to input correct value. In the second sample, you can do the same without using any exception, but you need to check up the result of the call.
The null value for format provider means using the current culture. In case of ',' as a thousands or hundreds separator, this is important, because some other cultures use '.' (or something else), to this format is culture-dependent, so it won't work in all cases. In you always want ',', you should rather specify some fixed culture, such as new
System.Globalization.CultureInfo("en-US", false);
please see:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx[
^].
—SA