6

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

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

And I try to convert it to double by doing:

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

And I obtain 5.0 insteads of 0.05.

Can you advice? What do I do wrong and how should I parse this?

pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • Similar issue: http://stackoverflow.com/questions/721950/double-parse-internationalization-problem – doblak Jul 12 '12 at 15:34
  • I suggest using `public static double Parse(string s,NumberStyles style,IFormatProvider provider)` instead. What exactly happens when you set a `string` variable to `ConfigurationManager.AppSettings["someValue"]` it seems you have not really tried to debug your code. – Security Hound Jul 12 '12 at 15:37

5 Answers5

9

That's for your culture settings, Test the same but with a comma instead a point and you will see that work's

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

To fixed this problem you could used the follow overload of the parse function

var d = double.Parse(ConfigurationManager.AppSettings["someValue"], CultureInfo.InvariantCulture);
Jorge
  • 17,896
  • 19
  • 80
  • 126
  • First that doesn't mean that i copy that, second i have question that i made few month ago with this problem. Because in my country We used the comma instead the point. http://stackoverflow.com/questions/10507759/convert-this-string-into-decimal. And Second when i edited my answer i was looking for the answer in that question to see the correct syntax for the overload – Jorge Jul 12 '12 at 15:42
  • 1
    @Jorge - So either way you just quoted somebody else and changed the values. Besides it would be much better to use the `public static double Parse( string s, NumberStyles style, IFormatProvider provider)` in a case like this. – Security Hound Jul 12 '12 at 15:45
  • @Jorge: That's fine, but you should reference the answer. –  Jul 12 '12 at 15:46
7

Maybe the problem is in the culture settings. There could be many issues with them, such as comma as digital separator. When you're working with non-cultured values, such as config files, you should explicitly specify that you need InvariantCulture. Try

var d = double.Parse(ConfigurationManager.AppSettings["someValue"],
                     CultureInfo.InvariantCulture);
Dmitry Osinovskiy
  • 9,999
  • 1
  • 47
  • 38
  • 4
    because some cultures treat `,` as fraction separator (decimal mark) and `.` as separator of every 3 digits – Alvin Wong Jul 12 '12 at 15:35
5

This code:

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

prints 5 as well, so the problem is in your culture settings.

Try

var d = double.Parse(
    ConfigurationManager.AppSettings["someValue"], 
    CultureInfo.InvariantCulture);
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

Always pass your culture info when using double.Parse. Here in Belgium, it's "0,05".

Carra
  • 17,808
  • 7
  • 62
  • 75
-1

It's because of culture settings. Please ensure "." is a delimiter in your current culture.

Raman Zhylich
  • 3,537
  • 25
  • 23