Converting string to float?

I’ve tried using the .ConvertTo method but it fails. Any ideas?

float mousex = (mousepositionstring[0].ConvertTo(float));

float.Parse or float.TryParse

Float.Parse also gives the error.

I ended up with:

float mousex = Single.Parse(mousepositionstring[0]);

For some reason this seems to be the only way to do it…

1 Like

Make sure you’re trying to parse a valid float string.
Also check the separator used… some cultures use “,” instead of “.”

I reccomend to use such way:

public void Start()
{
	string mousex_str = mousepositionstring[0];
	float mousex = GetFloat(mousex_str, 0.0F);
}

private float GetFloat(string stringValue, float defaultValue)
{
	float result = defaultValue;
	float.TryParse(stringValue, out result);
	return result;
}

Add Using System So it Works (:

That was a quite unnecessary bump of the thread after almost 10 years. The second post by DanielQuick already answered the question. It’s completely pointless to use System.Single.Parse since float is a direct alias name for System.Single.

So if someone tries to state that float.Parse does not work but System.Single.Parse does work, it’s a lie and that person didn’t really test it.

In Unity I would never recommend to add a using System; as the UnityEngine namespace contains several types which would clash with the System namespace. Most prominent cases are Debug and Random.

The “GetFloat” method that Patico suggested does not work the way he has implemented it. An out parameter ALWAYS has to assign a value to the variable. So assigning a default value before the TryParse call has no effect. If the parsing has failed, it assigns the value 0 by default, though this is not guaranteed. Officially the state is undefined. The method should look like this:

private float GetFloat(string stringValue, float defaultValue)
{
    if (float.TryParse(stringValue, out float result))
        return result;
    return defaultValue;
}
1 Like

This seems so much like it ought to work. In fact, if I pass it a string I manually create in code, it DOES work. But if I take the string from a Text Mesh Pro text field, it fails every time, even though logging the string out to the console results in exactly the same thing.

[EDIT] interestingly, only happens if I try to set it during Start, Awake or similar. Calling ForceUpdateCanvases doesn’t help either. Pity. I’ll just have to set the number in two places manually.

How did you log the string? Note that the TextMeshPro component should NEVER be used to read your input as the TextMesh component is only responsible for displaying the text and may contains invisible formatting characters.

In some rare cases people have reported that the actual input field also contains zero-width space character and using

.Trim((char)8203)

actually fixed it.

Though you just revived this thread without actually adding useful information. You seem to have a different situation compared to the OP. In 2013 we didn’t even had the TMP yet… So if you have something to add here, at least explain your actual usecase, what you actually did (show your actual code) and explain what happens and what you’ve done to debug it.

2 Likes

I think that’s enough necroing for one thread. Closed.