This line of code:
Console.WriteLine(Convert.ToInt32(“23,23”) + 1);
Throws an exception.
This line of code:
Console.WriteLine(Convert.ToDouble(“23,23”) + 1);
Prints 2324.
Does anybody know why this is the case? I wouldn’t think that anything good could come of the second conversion.
From the MSDN documentation of System.Double.Parse:
Here, the comma (
,) stands for “[a] culture-specific thousands separator symbol”.To summarize: If your current culture’s thousands separator symbol appears anywhere in the string, it is ignored by
Double.Parse(which is invoked internally byConvert.ToDouble).Int32.Parse(string), on the other hand, does not allow thousands separators in the string:
which is why your first example throws an exception. You can change this behavior for both
Double.ParseandInt32.Parseby using an overload that allows you to specifyNumberStyles, as explained by the other answers.