float.Parse()不工作,我想要的方式 [英] float.Parse() doesn't work the way I wanted

查看:116
本文介绍了float.Parse()不工作,我想要的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,我用将信息输入到我的application.The问题是,有些值是浮动,有时他们都为空,这就是为什么我得到一个异常。

I have a text file,which I use to input information into my application.The problem is that some values are float and sometimes they are null,which is why I get an exception.

        var s = "0.0";
        var f = float.Parse(s);



上面的代码抛出第2​​行异常输入字符串的不正确的格式。

The code above throws an exception at line 2 "Input string was not in a correct format."

我相信解决办法是float.Parse的先进的重载,其中包括的IFormatProvider作为参数,但我不知道这件事呢。

I believe the solution would be the advanced overloads of float.Parse,which include IFormatProvider as a parameter,but I don't know anything about it yet.

我如何解析0.0?

推荐答案

点符号。不被用作分离器(这取决于文化设置)。所以,如果你想绝对确保点被正确解析,你需要这样写:

Dot symbol "." is not used as separator (this depends on Culture settings). So if you want to be absolutely sure that dot is parsed correctly you need to write something like this:

CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.NumberFormat.CurrencyDecimalSeparator = ".";
avarage = double.Parse("0.0",NumberStyles.Any,ci);

这篇关于float.Parse()不工作,我想要的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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