C# float.Parse() 没有按照我想要的方式工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1014535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
float.Parse() doesn't work the way I wanted
提问by Ivan Prodanov
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);
The code above throws an exception at line 2 "Input string was not in a correct format."
上面的代码在第 2 行抛出异常“输入字符串的格式不正确”。
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.
我相信解决方案将是 float.Parse 的高级重载,其中包括 IFormatProvider 作为参数,但我对此一无所知。
How do I parse "0.0"?
我如何解析“0.0”?
采纳答案by inazaruk
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:
点符号“.” 不用作分隔符(这取决于文化设置)。因此,如果您想绝对确定 dot 被正确解析,则需要编写如下内容:
CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
ci.NumberFormat.CurrencyDecimalSeparator = ".";
avarage = double.Parse("0.0",NumberStyles.Any,ci);
回答by ChrisF
I've just tried this and it didn't throw any exceptions at all.
我刚刚试过这个,它根本没有抛出任何异常。
Is your number format using decimal comma rather than decimal point? Have you tried:
您的数字格式是否使用十进制逗号而不是小数点?你有没有尝试过:
var s = "0,0";
var f = float.Parse(s);
Having asked this I've just tried it with the comma expecting to get an exception, but didn't. So this might not be the answer.
在问过这个问题后,我刚刚用逗号尝试过,希望得到一个例外,但没有。所以这可能不是答案。
回答by Timotei
Or, you could simply check if the input text is not null, or empty.
或者,您可以简单地检查输入文本是否为空或空。
Also, be careful, because in some countries, the "." (dot) that separates the float numbers is "," (comma)
另外,要小心,因为在某些国家/地区,“。” 分隔浮点数的(点)是“,”(逗号)
回答by Richard
You can check for null or empty string first.
您可以先检查 null 或空字符串。
You can also use one of the overloads of Parse
(or even use TryParse
) to give more specific control.
您还可以使用Parse
(甚至使用TryParse
)的重载之一来提供更具体的控制。
E.g. to check using the invarient culture, to avoid decimal separator variations with non-user visible data (e.g. from A2A communications):
例如,使用不变区域性进行检查,以避免使用非用户可见数据(例如来自 A2A 通信)的小数分隔符变化:
float SafeParse(string input) {
if (String.IsNullOrEmpty(input)) { throw new ArgumentNullException("input"); }
float res;
if (Single.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out res)) {
return res;
}
return 0.0f; // Or perhaps throw your own exception type
}
回答by Jens van de M?tter
Following works for me:
以下对我有用:
string stringVal = "0.0";
float floatVal = float.Parse(stringVal , CultureInfo.InvariantCulture.NumberFormat);
The reverse case (works for all coutries):
相反的情况(适用于所有国家):
float floatVal = 0.0f;
string stringVal = floatVal.ToString("F1", new CultureInfo("en-US").NumberFormat);
回答by wlodi54
this should work.
这应该有效。
var s = "0.0";
var f = float.Parse(s, CultureInfo.InvariantCulture);
var s = "0.0";
var f = float.Parse(s, CultureInfo.InvariantCulture);