Dion Segijn
Dion Segijn

Reputation: 2655

While converting double 0.10 double.parse() converts to 10.0

I have a simple console application in visual studio for testing some code before going big. But now i have a problem with parsing some string to double.
When the users input is a String: 0.10 i want to convert this to a double. So the output should be a double: 0.10.

But when i do this with the following code:

double r_value = 0;
r_value = Math.Round(double.Parse(value), 2);

Or

r_value = double.Parse(value);

The output will be: 10 or 10.0. How can it be this output changes like this? and converts to 10.0 instead 0.10 as i thought it should be.

Upvotes: 3

Views: 1538

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500495

I strongly suspect your default culture is one in which . is a grouping separator (usually for thousands). So where an English person might write ten thousand as "10,000" some other cultures would write "10.000". At that point, your output makes sense.

If you want to parse with the invariant culture (which treats . as a decimal separator and , as the grouping separator) do so explicitly:

double r_value = double.Parse(value, CultureInfo.InvariantCulture);

Upvotes: 13

Related Questions