4

What's going on here?

Convert.ToDouble("1.3")

I have a string with the value "1.3". When I convert to double, it returns 13. I want to return 1.3.

alansiqueira27
  • 8,129
  • 15
  • 67
  • 111
  • 1
    Just a guess, is your regional format something like `$1.000.000,00` – ta.speot.is Apr 27 '14 at 20:10
  • 2
    What happens if you use the `Convert.ToDouble(string, IFormatProvider)` overload instead? – Martin Costello Apr 27 '14 at 20:11
  • 1
    @Seva In that case, you're formatting numbers using your local culture settings. Use the overload I suggested (and in the answer below), and it'll work as you expect. – Martin Costello Apr 27 '14 at 20:13
  • have you done _any_ research before asking this? – Selman Genç Apr 27 '14 at 20:14
  • @Selman22, do you earn reputation by only commenting these stuff? – alansiqueira27 Apr 27 '14 at 20:16
  • 2
    possible duplicate of [C# convert string to double in locale](http://stackoverflow.com/questions/12426978/c-sharp-convert-string-to-double-in-locale) **and** [How do I parse a string with a decimal point to a double?](http://stackoverflow.com/questions/1354924/how-do-i-parse-a-string-with-a-decimal-point-to-a-double) **and** [how to convert string to double with proper cultureinfo](http://stackoverflow.com/questions/2583362/how-to-convert-string-to-double-with-proper-cultureinfo) – Selman Genç Apr 27 '14 at 20:20
  • 2
    @Selman22 Locale-dependent questions are the sort of thing that most people have to encounter once as a "problem" before they realise what the question really is. – ClickRick Apr 27 '14 at 20:22

1 Answers1

11

It probably has to do with your current culture, it uses another decimal separator symbol than .. You can do this instead:

double.Parse("1.3", CultureInfo.InvariantCulture);
Magnus
  • 45,362
  • 8
  • 80
  • 118