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

查看:443
本文介绍了输入字符串的不正确的格式#2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 双温度;
临时=(双)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.

这篇关于输入字符串的不正确的格式#2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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