Word to the wise about parsing floats

If you are using float.Parse(string) or Double.Parse(string), this tidbit may save you some headache…

I had reports of weird-ass bugs with super-large characters and unresponsive analog sticks, and I realized that they were all coming from Europe. It turns out that numbers in my xml files were not being parsed correctly. I stored all numbers using decimal points to separate whole from fractional, as is done in English-speaking countries. However, in many countries, commas are used to separate them.

1.5

vs

1,5

Functions like float.Parse(string) will detect the “Culture” of the local system and parse accordingly by default. However, you can specify Culture.InvariantCulture as an additional parameter to ensure that it always parses numbers in the English way. This fixed some crazy bugs I was encountering.

Yes. This is a very common cause of bugs. It’s one of those issues that is widely known yet I think everyone gets bitten by it at least once.