|
-
Jul 7th, 2006, 08:35 AM
#1
Thread Starter
Hyperactive Member
[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!
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Jul 7th, 2006, 08:41 AM
#2
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.
-
Jul 7th, 2006, 09:16 AM
#3
Thread Starter
Hyperactive Member
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.
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Jul 7th, 2006, 09:24 AM
#4
Thread Starter
Hyperactive Member
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.
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
-
Jul 7th, 2006, 10:06 AM
#5
Addicted Member
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...
You dont need eyes to see, you need vision.
-
Jul 12th, 2006, 10:25 AM
#6
Thread Starter
Hyperactive Member
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!
____________________________________________
Please rate my messages. Thank you!
____________________________________________
Bram Vandenbon
http://www.bramvandenbon.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|