- Home /
Does parseFloat depend on the OS?
I am currently having some troubles with parseFloat(String). Here is some code to illustrate it:
pos.x = parseFloat("1,2345");
pos.x = parseFloat("1.2345");
The first one works against everything I found on the net correctly on my system (Win 7 64bit, german), but the second doesnt. It is the other way around for the one I am working together with, who lieves in the US and uses the US version of Win 7 64bit.
Is this possible, or am I just too stupid? It also seems as if converting a float to a string produces a comma on my system but a point on the us system...
Is there any better solution to this problem, then always checking the OS before parsing?
Thanks.
Answer by qJake · May 18, 2010 at 09:56 PM
Looks like you need to use the System.Globalization.NumberFormatInfo object in .NET. This lets you get the current (and correct) number separator (dot "." for US, comma "," for Europe) depending on the current machine's internationalization settings. You can then use this to parse numbers correctly, no matter what the language settings are on the OS.
Perfect, thank you. NumberFormatInfo.CurrentInfo.NumberDecimalSeparator is the OS dependant sign with which I can just replace the one in my string.
Answer by StephanK · May 18, 2010 at 09:53 PM
There's something called NumberStyles in .net. They are passed as flags to the parse function and should be able to deal with these kind of problems. Best you'd look them up on the msdn library.
Your answer
Follow this Question
Related Questions
comma on PC - dot on Mac 1 Answer
Rotation Values Garbled When Set By Code 2 Answers
Precision decimal data type 2 Answers
How do you convert Random.Range to two decimal places? 1 Answer