Number parsing weirdness

c#

Solution

From the MSDN documentation of System.Double.Parse:

The s parameter can contain [...] a string of the form:

`[ws][sign][integral-digits[,]]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]`

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 by `Convert.ToDouble`).

Int32.Parse(string), on the other hand, does not allow thousands separators in the string:

[ws][sign]digits[ws]

which is why your first example throws an exception. You can change this behavior for both `Double.Parse` and `Int32.Parse` by using an overload that allows you to specify `NumberStyles`, as explained by the other answers.

Problem

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.

Original source