How Do I Convert String To Float?

Self Explanatory.

float value = float.Parse(stringValue);
// or..
if (float.TryParse(stringValue, out float result))
{
}


polo thank you

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:
.


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.

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.

provided solution was still somewhat “not working”, so here it is my solution:

CultureInfo en_us = CultureInfo.GetCultureInfo( "en-US" );
float rotation_y = float.Parse( string_with_float_value_with_dot_as_decimal, en_us );