- Home /
Convert list of floats to float array
Hi,
I'm trying to convert a list of floats with elements separated by spaces to an array of floats. I thought this was pretty simple but the following doesn't seem to fill the array.
float[] data = Array.ConvertAll (ListOfFloats.Split(' '), float.Parse);
What am I doing wrong?
Thanks.
I don't see any issue with your line of code. Have you an example string that you use as input? Also it's important what language / local settings your operating system uses. For example in germany, france, spain and a few other countries the decimal seperator is the comma ins$$anonymous$$d of the point. float.Parse(string) will use your systems local settings.
Thanks. The separator is definitely the space and not the comma. The string is just a list of floats, e.g. "0.22 3.44 2.333". I have no idea what's wrong.
I did not talk about your seperator between your values, but the decimal seperator. In the US / GB as well as in normal C# the "dot" is the decimal seperator. However as i said in some countries that is not the case. Here are some examples:
2.333 // American number format
2,333 // German number format
If you have issues with some code i would highly recomment to first break it up into smaller pieces. That way you can debug each step.
Debug.Log("The input string: " + ListOfFloats);
string[] strings = ListOfFloats.Split(' ');
Debug.Log("Number of string elements: " + strings.Length);
float[] data = Array.ConvertAll (strings, float.Parse);
Debug.Log("Number of float elements: " + data.Length);
ps: Please do not use the Answer function on this site unless you actually want to answer your own question. I did not post an answer since your question is not clear and it's not clear what you have already done to debug your issue.
Thanks. I think you might be onto something re. the language settings as I am getting the following error -
System.FormatException: Input string was not in a correct format. at System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) [0x00083]
Is there a specific Unity setting to change numbering format? $$anonymous$$y OS is Australian.
Answer by Bunny83 · Feb 23, 2019 at 03:42 AM
The FormatException is thrown when you pass an empty string to float.Parse. So maybe your input string contains two spaces in a row which would lead to an empty string. You can try this one:
string[] strings = ListOfFloats.Split(new char[]{' '}, System.StringSplitOptions.RemoveEmptyEntries);
float[] data = Array.ConvertAll (strings, float.Parse);
If this still doesn't help, you may want to further split the code and do the array creation manually
string[] strings = ListOfFloats.Split(new char[]{' '}, System.StringSplitOptions.RemoveEmptyEntries);
float[] data = new float[strings.Length];
for(int i = 0; i < strings.Length; i++)
{
Debug.Log("i: " + i + " Parsing: >"+strings[i]+"< ("+strings[i].Length+")");
data[i] = float.Parse(strings[i], System.Globalization.CultureInfo.InvariantCulture);
}
This will always use the invariant culture (so always use the "." dot as decimal point). If the string could contain other wrong information like letters of symbols you may want to use TryParse instead
TryParse will not throw an exception but will simply return false when the conversion fails. The actual value is returned through an out parameter.
Your answer
Follow this Question
Related Questions
Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers
Is there a way to remove array entries in the editor? 4 Answers
Array of Lists null exception when adding to list. 2 Answers
Argument if out of range: loops and arrays 2 Answers
Multidimensional Array of Vector2 2 Answers