双温度;
临时=(双)Convert.ToDouble(1234.5678);
嗨伙计们和女士们,我不能为我的生命弄清楚为什么上面的线是行不通的。上面一行给了我一个运行时错误,说;
类型的未处理的异常 System.FormatException发生在 mscorlib.dll中
信息:输入字符串 不正确的格式。
解决方案据我所知转换
方法使用当前区域做这样的转换。在这种情况下,我猜你现在的语言环境会期望一个逗号作为小数点。尝试设置当前的区域设置为你的应用程序或转换成某种语言/国家,点是使用(如EN_US)。该方法应当提供第二可选参数,以提供的IFormatProvider作为一种替代的解决方案。
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.
解决方案
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.