Can't convert string to double!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I got :
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.

at execution of simple line:
double v1 = Double.Parse ("7200.0");

What's wrong?
 
zelyal said:
I got :
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.

at execution of simple line:
double v1 = Double.Parse ("7200.0");

What's wrong?

Almost certainly you're using the wrong CultureInfo. Try specifying
CultureInfo.InvariantCulture to parse in - I suspect it's currently
assuming that "." is a thousands separator rather than a decimal point.

Jon
 
What version of the framework are you using? I copied your line of code to a
..Net 2.0 project and it ran without exceptions.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Abnormality is anything but average.
 
Specify the number format or culture to use. If you don't do that, you
are using the number format of the default culture.

double v1 = Double.Parse("7200.0", CultureInfo.InvariantCulture);
 
Verify that the decimal separator is '.' and not something else (like ','
for example). You can use the
System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
property to check it in code

/claes
 
I got :
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.

at execution of simple line:
double v1 = Double.Parse ("7200.0");

What's wrong?

Culture, I suspsect. Are you in a country which doesn't use "." as its
decimal separator...?
 
The problem is in separator. Dot sign leads to error, but with comma sign
all OK.
I tried:
double v1 = Double.Parse ("7200.0", NumberStyles.Float);
but it didn't help. And
v1 = Double.Parse("7200.00", CultureInfo.InvariantCulture);
is gone OK.
Thank to all and specially to Göran Andersson !
Alexey
 
A clean way to handle this kind of situation is the Double.TryParse().

~ John Fullmer
 
That prevents the code from causing an exception, but it doesn't solve
the actual problem, which is the number format used to parse the string.
 
Back
Top