输入字符串的格式不正确 #2 [英] 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;
未处理的异常类型System.FormatException 发生在mscorlib.dll
An unhandled exception of type System.FormatException occurred in mscorlib.dll
附加信息:输入字符串格式不正确.
Additional information: Input string was not in a correct format.
推荐答案
据我所知,Convert
方法使用当前的语言环境进行此类转换.在这种情况下,我猜您当前的语言环境会期望逗号作为小数点.尝试为您的应用程序设置当前区域设置或转换为使用点的某些语言/国家/地区(例如 en_US).该方法应提供第二个可选参数以提供 IFormatProvider 作为替代解决方案.
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.
这篇关于输入字符串的格式不正确 #2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!