Hi, I’m trying to import a .csv which contain floats, but Unity seems to have some trouble reading it. When I import a simple .csv sheet with integers only, everything works fine. But when I import the same sheet with floats instead of integers, I get the error:FormatException: Input string was not in a correct format. Is there any way to fix this?
EDIT: After some experiments, I realized that my script failed to reed line 3, which contains a minus, but line 1 and 2 are fine.
Have you tried printing out the exact string it’s choking on? That would help a great deal in diagnosing this issue.
Also inspecting the raw CSV file in a plain text editor might be revealing too.
It’s probably the (current) culture that is used by the parse method which appears to expect a different separator, most-likely a comma. You either need to change the separator for the purposes of formatting, or supply a format provider that supports dots.
If that’s not the issue, you need to do some more testing.
I just did it, in the plain text editor it looks like this
Its not the same sheet, but its the same kind of values with the same number of rows.
When I print it:
I just tried parsing that exact string:
Debug.Log(float.Parse("-0.494323"));
This worked completely fine for me. I think @Suddoha is probably correct about the culture being the issue. You may be using a culture that is expecting a comma to denote the decimal point. So maybe try something like this:
float.Parse(components[1], CultureInfo.InvariantCulture);
This is good practice anyway because your code could otherwise break based simply on the user’s device settings.
It’s working! Thank you so much!