Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dkimboyle · Feb 23, 2019 at 01:48 AM · arrayslists

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 23, 2019 at 01:57 AM 0
Share

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.

avatar image dkimboyle · Feb 23, 2019 at 02:02 AM 0
Share

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.

avatar image Bunny83 dkimboyle · Feb 23, 2019 at 02:33 AM 0
Share

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.

avatar image dkimboyle Bunny83 · Feb 23, 2019 at 03:00 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges