And ...">

Why does double.Parse ("0.05") return 5.0?

I am reading the value from my App.config; which the:

 <add key="someValue" value="0.05"/>

And I'm trying to convert it to double by doing:

 var d = double.Parse(ConfigurationManager.AppSettings["someValue"]);

And I get 5.0 instead of 0.05.

Can you advise? What am I doing wrong and how should I disassemble it?

+5
source share
5 answers

This is for your culture settings, check the same thing, but specify a period instead of a comma, and you will see that

var d = double.Parse("0,05");

To fix this problem, you could use the following parsing function overload

var d = double.Parse(ConfigurationManager.AppSettings["someValue"], CultureInfo.InvariantCulture);
+8
source

, . , , . , , , InvariantCulture.

var d = double.Parse(ConfigurationManager.AppSettings["someValue"],
                     CultureInfo.InvariantCulture);
+6

:

var nfi = new NumberFormatInfo {
    NumberGroupSeparator = ".",
    NumberDecimalSeparator = ","
};
Console.WriteLine(double.Parse("0.05", nfi));

5, .

Try

var d = double.Parse(
    ConfigurationManager.AppSettings["someValue"], 
    CultureInfo.InvariantCulture);
+4

double.Parse. , , "0,05".

-1

- . "." .

-2

All Articles