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?
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);
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);
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);
Always pass your culture info when using double.Parse. Here in Belgium, it's "0,05".
It's because of culture settings. Please ensure "." is a delimiter in your current culture.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.