- Home /
float.Parse not working in iOS/xCode
Hi, hope someone can clarify this for me.
I have a script in which I get a string like this from my database: string number = 72,1
I then need to make the comma a punctation and do it like this: number.Replace(",", ".");
I then need to make this into a float, double or decimal for calculation. I use this: float newNumber = float.Parse(number);
This has been working just fine until recently. In my Unity editor and when running it there it works like a charm: Float: 72.1 Double: 72.1 Decimal: 72.1
But when building to iOS through xCode it removes the punctation: Float: 721 Double: 721 Decimal: 721
Can anyone please explain why this happens?!?!
Unity Version: 2020.1.1f1 xCode vVersion: 11.6
Answer by Bunny83 · Aug 11, 2020 at 02:47 PM
Well, since your database value is "72,1" I assume you are from one of these countries? The issue might be that float.Parse uses the devices local culture settings by default. Maybe you have you use a different culture on your PC or have changed the seperator settings in your operating system manually so it works there. I've actually done this myself. My system language and culture is set to german but I changed the decimal seperator from comma to point since I never really have to deal with the german number format.
Anyways since you "normalized" your number manually by replacing the comma with a point you should simply use the invariant culture. This culture has been invented specifically for this case. The invariant culture is pretty much the same as the english culture setting but it's guaranteed that it will never change (which is not necessarily true for any real culture).
Just add this line at the top of your script:
using System.Globalization;
and then use
newNumber = float.Parse(number, CultureInfo.InvariantCulture);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to open an internal file .vcf in C # to iPhone? 0 Answers
How can i modify a float in another script in C#? 1 Answer
HttpWebResponse and JIT on iOS 2 Answers