简体   繁体   中英

System.FormatException when trying to convert String to Double

I got this error when I'm trying to convert String into Double on WP7 C#.

tokenvalue = Convert.ToDouble(saParsed[i].Replace(".", ","));

I getting this error in WP7. A first chance exception of type System.FormatException occurred in mscorlib.dll

Is there any way to avoid it or is it only a fault of Emulator?

First you can try to use this:

double tokenvalue = Convert.ToDouble(saParsed[i], CultureInfo.InvariantCulture);

Anyway you'd better check if it's ok:

double tokenvalue;
if (Double.TryParse(saParsed[i], out tokenvalue) 
{ 
    // Do what you please here
}

Try something like this.

var tokenvalue = Convert.ToDouble(saParsed[i]);
var tokenValueText = tokenValue.ToString().Replace(".", ",");

Hope it will work fine if saParsed[i] is holding the valid double value.

Try to convert it with the following statement:

double tokenvalue; 
if (double.TryParse(saParsed[i], NumberStyles.Any, 
    NumberFormatInfo.CurrentInfo, out tokenvalue))
{  
    // Convertion was successfull
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM