Wrong string to double conversion

Asked

Viewed 159 times

2

I have in my app.config the following code:

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

And here 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, I just need those values to be doubles in the same way as they are, and in the conversion is returning totally different values.

Ex: 6.36413684210526 converts to -> 6364136842105.26

Does anyone know why of this? how do I convert the number to double that he remains the same?

  • Are you sure these values are going right for the .Select()? I made a Fiddle with them.

  • @Gypsy omorrisonmendez I think your filde is not working no.

  • Already tried to convert to Decimal?

  • Doing what is in the answer below function. just put the CultureInfo.InvariantCulture.

1 answer

2


This is because the culture that is using does not consider the point as decimal separator (PT-BR). To avoid this, you must define a culture that you consider. You can use as an example the code below.

 return valor.Select(i => Convert.ToDouble(i, CultureInfo.InvariantCulture)).ToArray();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.