Click here to Skip to main content
15,888,210 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is my code i am going to convert double at the time showing error
C#
Pointk pt1 = new Pointk(Convert.ToDouble(S3[1].ToString()), Convert.ToDouble(S3[0].ToString()));


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

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


how to fix it any body help me

What I have tried:

This is my code i am going to convert double at the time showing error
Pointk pt1 = new Pointk(Convert.ToDouble(S3[1].ToString()), Convert.ToDouble(S3[0].ToString()));

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

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


how to fix it any body help me
Posted
Updated 18-Mar-16 23:43pm
v2
Comments
Sascha Lefèvre 19-Mar-16 4:29am    
Of which type is the array S3?

We can't tell you how to fix this: it depends on a number of factors, such as what type your S3 array returns, and what the resulting string contains prior to conversion.
So change your code slightly for testing:
C#
string sd1 = S3[1].ToString();
double dd1 = Convert.ToDouble(sd1);
string sd2 = S3[0].ToString();
double dd2 = Convert.ToDouble(sd2);
Pointk pt1 = new Pointk(dd1, dd2);
Then run your code in the debugger. When the exception happens, use the debugger to look at the content of the intermediate variables and work out exactly what you are trying to convert.
If should be fairly obvious from that why one of the conversions is failing.
But we can't do that for you - we don't have your data!


how to convert this string to double "-97.08215655572718"


Well...
C#
string sd1 = "-97.08215655572718";
double dd1 = Convert.ToDouble(sd1);

Works fine for me!
But I don't like Convert.ToAnything - it's generally a lot better to use the relevant TryParse method and report or log problems rather than just let your app crash.
So i'd write your code as:
C#
string sd1 = S3[1].ToString();
double dd1;
if (!double.TryParse(sd1, out dd1))
    {
    // Report or log problem
    return;
    }
Or more likely I'd see exactly what was in my array to start with and avoid strings altogether unless I really, really had to. I keep values in "native" form as much as possible, and cast where I have to rather than rely on string conversions.
 
Share this answer
 
v2
Comments
Member 5833550 19-Mar-16 5:14am    
how to convert this string to double "-97.08215655572718"
OriginalGriff 19-Mar-16 5:38am    
Answer updated.
The exception indicates that the string is not a number in a valid format. As already suggested you should check your strings (and show them here).

Common sources for such exceptions are culture-specific formatting. When you pass a string using the period as decimal point, conversion will fail when the actually used locale uses a different decimal point character like the comma. This can be handled by using a convert function that has an additional parameter for the culture: Convert.ToDouble Method (String, IFormatProvider) (System)[^].

An alternative that I use in many of my programs is checking if the locale uses a decimal point character that is not a period and if so replace periods in strings to be converted to double by the correct character before conversion. This allows users to enter numbers with the period and importing data from other sources. But note that this will only work for strings without group separators.

Example:
C#
string decimalPoint = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
if (decimalPoint != ".")
    str.Replace(".", decimalPoint);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900