Search Unity

How Do I Convert String To Float?

Discussion in 'Scripting' started by SealBarkStudios, Aug 16, 2022.

  1. SealBarkStudios

    SealBarkStudios

    Joined:
    May 21, 2022
    Posts:
    19
    Self Explanatory.
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Code (CSharp):
    1. float value = float.Parse(stringValue);
    2. // or..
    3. if (float.TryParse(stringValue, out float result))
    4. {
    5. }
     
    Fenikkel and SealBarkStudios like this.
  3. SealBarkStudios

    SealBarkStudios

    Joined:
    May 21, 2022
    Posts:
    19

    polo thank you
     
    adamgolden likes this.
  4. rogerwalms

    rogerwalms

    Joined:
    Sep 15, 2023
    Posts:
    1
    I had to answer this, because I could find no answer anywhere. I tried Parse and TryParse. I wrote my string to the Debug Log and saw that the string contained only a number. So, I tried the Try Catch and found the following error:
    . upload_2023-9-22_8-32-33.png
    Here is my solution:
    OKtoFire = true;
    string angle1 = angleInput.GetComponent<TMP_Text>().text;
    int lngth = angle1.Length;
    string angleIn = angle1.Substring(0, lngth - 1);
    try
    {
    launchAngle = float.Parse(angleIn, System.Globalization.NumberStyles.Float);
    //launchAngle = System.Convert.ToSingle(angleIn);
    }
    catch(Exception ex)
    {
    OKtoFire = false;
    Debug.LogException(ex, this);
    Debug.Log("angle: " + angleInput.GetComponent<TMP_Text>().text);
    }
    I get the length of the string, then take the Substring from 0 to length - 1.

    I hope this helps somebody, because I spent a whole day trying to debug this error.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    It might help somebody, but what you did is not exactly a revolution in computer programming.
    First of all, you haven't shown what kind of string produced the formatting error.

    For the numerical strings to be parsed at all, there are some syntactic constraints in place. One of them is for a string to contain numerical characters and likely to not begin with a non-numeric character (the exact syntax is shown below). The other is, well, your local culture setting which might include a different character to represent a decimal dot, in many cultures it is a comma instead of a dot, and so swapping the two is also likely to produce an error.

    Here's a reference to float.Parse specification.
    These are some of the errors you get when you use float.Parse wrongly
    s is null => ArgumentNullException
    s does not represent a numerical value => FormatException
    s represents a number that can't fit into 32-bit float => OverflowException

    What you've shown is a FormatException, and I wish you provided a snippet of code, to show us what exactly causes the error, but quite probably your string starts with letters, which is simply invalid if you want to parse it as a floating point numeral, which is assumed to have the following syntax
    What you're doing with Substring also makes very little sense in that context. Yes, you definitely need to remove the textual part from your number, but Substring(0, text.Length - 1) makes no sense, you get the same string back.

    And use of Try/Catch is completely redundant here when you can simply use TryParse to a greater effect and better performance.

    I would advise you to read the manual on float.Parse, float.TryParse, NumberStyles, and InvariantCulture, and please try using code tags next time.
     
    Last edited: Sep 23, 2023