Wrong string conversion for double

2

I have in my app.config the following code:

<add key="FolhaA4" value="0.168056514197457, 6.36413684210526"/>

And here's the method to get these key values:

 private double[] ObterValoresConfiguracao(string chave)
 {
     Manager.Configuration config = Manager.ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
     Manager.AppSettingsSection app = (Manager.AppSettingsSection)config.GetSection("appSettings");

     string[] valor = app.Settings[chave].Value.Split(',');
     return valor.Select(i => Convert.ToDouble(i)).ToArray();
  }

I'm retrieving them as string and converting to double , but I need these values to be doubles the same as they are, and the conversion is returning totally different values.

Ex: 6.36413684210526 converts to - > 6364136842105.26

Does anyone know why this is so? how do I convert the number to double that it stays the same?

    
asked by anonymous 23.07.2015 / 21:42

1 answer

2

This occurs because the culture you are using does not consider the dot as decimal separator (PT-BR). For this to not happen, you must define a culture that you consider. You can use the code below as an example.

 return valor.Select(i => Convert.ToDouble(i, CultureInfo.InvariantCulture)).ToArray();
    
23.07.2015 / 21:51