Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Unable to Parse String to Float

Discussion in 'Scripting' started by taskmagee, Feb 3, 2023.

  1. taskmagee

    taskmagee

    Joined:
    Sep 16, 2018
    Posts:
    31
    I know this topic has been talked about a lot. But all the posts I've read and suggestions I've seen and tried haven't worked. Below is a screenshot of the error and a part of the code trying to parse the string to a float. I've confirmed that the string doesn't have any spaces or special characters as you can see from the debug log in the screenshot. Any help with the would be wonderfull because I am at a complete loss.
    Code (CSharp):
    1.  
    2. string rate = "";
    3. for (int x = 0; x < roles.Length; x++)
    4. {
    5.    if(employees[i].jobRole == roles[x])
    6.    {
    7.         rate = rates[x];
    8.    }
    9. }
    10. float numericRate = float.Parse(rate);
     

    Attached Files:

  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780

    Have you tried '.' vs ',' ?
     
  3. taskmagee

    taskmagee

    Joined:
    Sep 16, 2018
    Posts:
    31
    I have. I also tried CultureInfo.InvariantCulture
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    This is very frequently a localization/culture issue. Your computer may be set to a locale that uses
    ,
    instead of
    .
    as the decimal separator.

    You can and should parse the data with a specific CultureInfo, e.g.
    CultureInfo.InvariantCulture
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Could there be spaces at the start/end of the string?
    Try trimming the string.
     
  6. taskmagee

    taskmagee

    Joined:
    Sep 16, 2018
    Posts:
    31
    I've tried that and that isn't working.

    I just tried to tim the start and end of the string and I'm getting the same error
     
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Sounds like there's (still) some type of weird char in that string then.
    The string-length would probably not be 5 (2, 2, ., 8, 5) in that case, but 6 or more.

    Either that, or one of those 'digits' isn't actually a digit, but a funky character.

    As a sanity-check; have you tried running float.Parse("22.85"); directly?
     
  8. taskmagee

    taskmagee

    Joined:
    Sep 16, 2018
    Posts:
    31
    So I just did a debug on the length of the string and I am in fact getting one more character than expecting. It should be 5 for 22.85 but the debug shows 6. Here is the code that I used for trimming. Please tell me if it's incorrect.
    Code (CSharp):
    1. string rate = "";
    2.                 for (int x = 0; x < roles.Length; x++)
    3.                 {
    4.                     if(employees[i].jobRole == roles[x])
    5.                     {
    6.                         rate = rates[x];
    7.                     }
    8.                 }
    9.                 Debug.Log(rate.Length);
    10.                 rate = rate.TrimStart();
    11.                 rate = rate.TrimEnd();
    12.                 float numericRate = float.Parse(rate, CultureInfo.InvariantCulture);
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
  10. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Your trim is fine. There's probably some sort of non-ASCII-character in there that the console cannot display.
    Try printing all of the bytes in the string, one by one (their value). One of them will be (far) outside the 0-9-range (aside from the '.')


    Edit: To clarify, the 0-9-range in ASCII is from 48-57. The byte-value for '.' should be 46, the one for ',' should be 44.
     
    PraetorBlue likes this.
  11. taskmagee

    taskmagee

    Joined:
    Sep 16, 2018
    Posts:
    31
    How do I do that. I found a snippet online but the print result I'm getting are like 50, 46, 56
    Code (CSharp):
    1.                 byte[] bytes = Encoding.ASCII.GetBytes(rate);
    2.                 foreach (byte b in bytes)
    3.                 {
    4.                    Debug.Log(b);
    5.                 }
     
  12. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Yeah.. Compare that to a ASCII-table:
     
  13. taskmagee

    taskmagee

    Joined:
    Sep 16, 2018
    Posts:
    31
    I figured it out. There's a question mark in the string that isn't displaying when I debug. I just need to figure out how to remove that now. Thank you so much for the help with this. I've been banging my head for 2 days
     
  14. taskmagee

    taskmagee

    Joined:
    Sep 16, 2018
    Posts:
    31
    Just one final post before switching this to solved. In case anyone comes across this with the same issue I found this line of code which removed all hidden characters from the string.
    Code (CSharp):
    1. using System.Text.RegularExpressions
    2.  
    3. rate = Regex.Replace(rate, @"\p{C}+", string.Empty);
     
    SF_FrankvHoof likes this.