views:

60

answers:

5

Hi there, I need to convert a string to a float. These is is my example string:

1 MW +00000.00 mm
2 MW +0000.000 mm
3 MW -00000.01 mm
4 MW +00000.00 mm
5 MW +00002.92 mm
6 MW +00002.69 mm

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

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.

+4  A: 

You need to pass the CultureInfo for the culture that the strings are formatted in.

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

The example from MSDN would be:

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

PaulG
Here is another SO post on how you do it. http://stackoverflow.com/questions/3059587/c-float-to-string-conversion-decimal-separator-problem
Dustin Laine
okay i have a question here... if i pass the CultureInfo for let's say, english, will it be "." on english and "," on spanish? thanks
Richard González Alberto
@Richard. Maybe I'm misunderstanding the question. Are the *input* strings formatted differently per culture?
PaulG
nevermind... just got it ;) thanks ;)
Richard González Alberto
A: 

You will need to use the TryParse overload that allows you to specify the Culture, you will want to get the Culture info for en-us and then you can have it parse the same for everyone.

Mitchel Sellers
+1  A: 

Use 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.

Robert Harvey
thanks, this helped! :)
Richard González Alberto
A: 

You could try implementing by using the ToSingle method which is essentially the Alias "Float"

in C#.

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

I agree with Robert Harvey as he points out the Culture info and using Tryparse overload. Good suggestion!

Nightforce2
Are you a imbecile? He was asking for a "String-to-float Conversion". Looking at the code and adding a correct conversion from a string to float is a correct answer as "ToSingle" does just that!!!! and Roberts suggestion fixes the culture problem. I felt it was not needed to be a full answer as it was already answered above. For this reason I give you a -1!
Nightforce2
He was asking for a "String-to-float Conversion". Looking at the code and adding a correct conversion from a string to float as "ToSingle" does just that!!!! Which was requested and Roberts suggestion fixes the culture problem w/"Spanish". I felt it was not needed to be redundant in my response as it was already answered above. For this reason your logic is wrong and you also need a grammar check. You clearly don't see the picture. Besides Why would I need to gold plate this answer for your understanding when YOU voted down? If you don't understand correct conversions you shouldn't be judging
Nightforce2
A: 

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+].

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!

Homam