- Home /
how to use a float in a GUI.TextField?
Using C#, in the OnGUI function I just have something simple like this:
someFloatMemberVariable = float.Parse(GUI.TextField(someRect, someFloatMemberVariable.ToString()));
someFloatMemberVariable is of type float and is initialized to say 1.5, but when I type in the textfield another float, the decimal won't show up, and I always get a FormatException.
Any thoughts?
I think when you get as far as typing "12." then the decimal point would be stripped by the parsing operation. You would have the same problem with trailing zeroes after the decimal. I suggest you let the user type into a string variable, then parse that into your float, rather than continuously converting the float to a string. Not sure about the format exception though.
Answer by stfx · Mar 22, 2012 at 10:07 AM
The trick here is to use a string format for the variable which forces the decimal point. Like so:
public float Length;
void OnGUI()
{
string lengthText = GUILayout.TextField(Length.ToString("0.00"));
float newLength;
if (float.TryParse(lengthText, out newLength))
{
Length = newLength;
}
}
EDIT: It is also possible by adding a bool to store if a . is entered. Downside is that it needs a further variable:
public float Length;
bool _isLengthDecimal;
void OnGUI()
{
string lengthText = GUILayout.TextField(Length.ToString() + (_isLengthDecimal ? "." : ""));
_isLengthDecimal = lengthText.EndsWith(".");
float newLength;
if (float.TryParse(lengthText, out newLength))
{
Length = newLength;
}
}
Thanks, stfx. This helped me out in controlling animation speed using a textfield. I used...
animSpeed = float.Parse(GUI.TextField(Rect(10, 10, 100, 20), animSpeed.ToString("0.00")));
edit I still get a FormatException error when typing "0.1" with "$$anonymous$$ char: ." as the culprit. The value is still valid as far as functionality is concerned, but I don't like that the error is generated. I have to type "0" then move the cursor behind the "." and then type "1" to keep from getting the error. Not the most intuitive way to input text into a field...
If you really don't want to have a .00 forced at the end but also don't want to have a seperate variable, you can try this. Very costly though...
Answer by mrde · Mar 01, 2011 at 05:11 PM
when you trying to parse to float and parse value is not valid ("3.5g", "4.k3", ...), it throw exception you can write follow
try {
someFloatMemberVariable = float.Parse(GUI.TextField(rect, someFloatMemberVariable.ToString()));
} catch {}
Yes, I know about try/catch and how it works. That doesn't solve my problem though.
Answer by trothmaster · Oct 06, 2014 at 06:08 PM
Here is how I solve this. I "clean" the string using regular expression You will need this at the top of your class:
using System.Text.RegularExpressions;
And here are two methods. One that will clean a float and another an int:
public static string CleanStringForFloat(string input)
{
if(Regex.Match(input,@"^-?[0-9]*(?:\.[0-9]*)?$").Success)
return input;
else {
Debug.Log("Error, Bad Float: " + input);
return "0";
}
}
public static string CleanStringForInt(string input)
{
if(Regex.Match(input,"([-+]?[0-9]+)").Success)
return input;
else {
Debug.Log("Error, Bad Int: " + input);
return "0";
}
}
To use them you would just do this:
string testFloat = Guilayout.TextField(testFloat);
testFloat = CleanStringForFloat(testFloat);
Your answer
![](https://koobas.hobune.stream/wayback/20220613110827im_/https://answers.unity.com/themes/thub/images/avi.jpg)