Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input string was not in a correct format #2

double temp;
temp = (double)Convert.ToDouble("1234.5678");

Hey Lads and Ladies, I can't for the life of me figure out why the above line isn't working. The above line gives me a runtime error that says;

An unhandled exception of type System.FormatException occurred in mscorlib.dll

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

like image 826
Keith Loughnane Avatar asked Mar 11 '11 16:03

Keith Loughnane


People also ask

What is an input string?

String input templates allow you to specify the type, order, and number of characters a user can enter in a data entry field. You can define string input templates only on single‑line character data entry fields.

What is string format in Java?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Syntax: There is two types of string format() method.


2 Answers

As far as I know the Convert methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.

like image 132
Mario Avatar answered Oct 14 '22 13:10

Mario


In order to convert string to double without an exception:

An unhandled exception of type System.FormatException occurred in mscorlib.dll

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

make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:

double.Parse("1234.5678", CultureInfo.InvariantCulture) 
like image 24
Vadim Gremyachev Avatar answered Oct 14 '22 12:10

Vadim Gremyachev