[RESOLVED] float.Parse() should always take "." as decimal seperator.
Hi,
I am trying the following
String str = "1.3";
float f = float.Parse(str);
On an american system this will work perfectly.
result: f = 1.3
However in some european countries "." is not the decimal seperator but "," is the decimal seperator.
result: f = 13
I need to get this code running on all platforms though. I could ofcourse just replace the "," and "." signs. But then it would no longer run on american systems.
How is this problem normally fixed? In my program "." should always be seen as a decimal seperator.
This is important because I am reading files with coordinates that need to be parsed correctly. In these files (*.x3d) the "." sign is the decimal seperator.
Thanks in advance for all help!
Re: float.Parse() should always take "." as decimal seperator.
You use ParseExact if you want to specify an absolute format. I think that should work in this case.
Re: float.Parse() should always take "." as decimal seperator.
Thanks for your quick reply!
Strange no such class or function with name "ParseExact".
Hmm, I'll try to write a class that implements IFormatProvider in mean time.
Re: float.Parse() should always take "." as decimal seperator.
Code:
System.Globalization.NumberFormatInfo nmbrstyle = new System.Globalization.NumberFormatInfo();
nmbrstyle.NumberDecimalSeparator = ".";
nmbrstyle.NumberGroupSeparator = ",";
float.Parse(str, nmbrstyle);
I'll try with this. Maybe this works. NumberFormatInfo seems to already implement IFormatProvider.
Re: float.Parse() should always take "." as decimal seperator.
hi, take a look at this
Code:
CultureInfo currentCulture = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentUICulture = currentCulture;
Console.WriteLine(currentCulture.NumberFormat.NumberDecimalSeparator);
string strDouble = "3,4";
double dblDouble = Double.Parse(strDouble,currentCulture.NumberFormat);
Console.WriteLine(dblDouble);
This produces the correct result...
Re: float.Parse() should always take "." as decimal seperator.
Thanks for all your replies.
But in fact the last code I posted worked perfectly.
This thread is resolved.
Thanks!