输入字符串的格式不正确 #2 [英] Input string was not in a correct format #2

查看:25
本文介绍了输入字符串的格式不正确 #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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆