简体   繁体   English

C#String to Float Conversion

[英]C# String to Float Conversion

I need to convert a string to a float. 我需要将字符串转换为float。 These is is my example string: 这是我的示例字符串:

1 MW +00000.00 mm 1兆瓦+00000.00毫米
2 MW +0000.000 mm 2兆瓦+0000.000毫米
3 MW -00000.01 mm 3 MW -00000.01 mm
4 MW +00000.00 mm 4兆瓦+00000.00毫米
5 MW +00002.92 mm 5兆瓦+00002.92毫米
6 MW +00002.69 mm 6兆瓦+00002.69毫米

And this is what i'm doing: 这就是我正在做的事情:

text = text.Substring(pos + 5, 9).Trim();                  
float val = 0.0F;
float.TryParse(texto, out val);
this.txtDimension1.Text = val.ToString();

Okay, this works for my environment, which is en_US, but when i run this same piece of code in an Spanish environment, it converts -00000.01 to -1.0 好的,这适用于我的环境,即en_US,但是当我在西班牙语环境中运行同一段代码时,它会将-00000.01转换为-1.0

I think it's a comma problem, in english numbers are separated by a dot (".") and spanish they are separated by a comma (","). 我认为这是一个逗号问题,英文数字用点(“。”)和西班牙语分隔,用逗号(“,”)分隔。

How can i make this work on both langs? 我怎样才能在这两个语言上做这个工作?

Thanks, Richard. 谢谢,理查德。

You need to pass the CultureInfo for the culture that the strings are formatted in. 您需要传递CultureInfo以获取格式化字符串的文化。

http://msdn.microsoft.com/en-us/library/3s27fasw.aspx http://msdn.microsoft.com/en-us/library/3s27fasw.aspx

The example from MSDN would be: MSDN的例子是:

double number;

string value = "1,097.63";
NumberStyles style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);

Alternatively, if your input strings are formatted differently then use CultureInfo.CurrentCulture 或者,如果输入字符串的格式不同,则使用CultureInfo.CurrentCulture

Use CultureInfo.InvariantCulture. 使用CultureInfo.InvariantCulture。

float.TryParse(texto, NumberStyles.Any, CultureInfo.InvariantCulture, out val);

If the input strings can vary, you will have to detect that and match up your input with the correct culture. 如果输入字符串可能不同,则必须检测输入字符串并将输入与正确的文化匹配。

You could try implementing by using the ToSingle method which is essentially the Alias "Float" 您可以尝试使用ToSingle方法实现,该方法基本上是Alias“Float”

in C#. 在C#中。

float val = (float)System.Convert.ToSingle(text);

I agree with Robert Harvey as he points out the Culture info and using Tryparse overload. 我同意Robert Harvey指出文化信息并使用Tryparse重载。 Good suggestion! 好建议!

It's not an answer for the question, but I added it as an answer just to show the code.. 这不是问题的答案,但我将其作为答案添加,只是为了显示代码..

I just want to alert you about using Substring , It'll may cause a problem when the first number become more than 9 [Two chars+]. 我只想提醒你使用Substring ,当第一个数字超过9 [两个字符+]时,它可能会导致问题。

I think using regular expression is better in this case, and this is the expression that you need: 我认为在这种情况下使用正则表达式更好,这是您需要的表达式:

string str = @"4 MW +12345.67 mm";
Regex r = new Regex(@".* MW (?<number>.+) mm", RegexOptions.IgnoreCase);
var match = r.Match(str);
string matchedString = string.Empty;
if (match.Success)
    matchedString = match.Groups["number"].Value;

Console.WriteLine(matchedString);

Note: you can improve the quality of the expression by check the value if it's a flow number, but I think this is enough for your situation. 注意:您可以通过检查值来提高表达式的质量,如果它是流数,但我认为这对您的情况就足够了。

Good luck! 祝好运!

您将需要使用允许您指定文化的TryParse重载,您将需要获取en-us的文化信息,然后您可以让它为每个人解析相同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM