Convert Float to a String

Hey guys,

I have found loads of resources coverting from a string to a float or int (parsing, etc), but I can’t seem to make it work the other way. I have no doubt missed something fairly obvious, but I can’t seem to work this one out.

It is for a GUI horizontal slider, I want the results of the float of the slider to be displayed as a GUI.Label as a reference point (its for creating a new building, measuring in “metres”).

Any help hugely appreciated!

1 Like
C# code
float x = 0.1f;
Debug.Log("val: " + x.ToString());

its prob more or less the same in unity script

2 Likes

Cheers perlohmann, I will give it a shot.

Works like a charm, here’s how it ended up.

heightSliderValue = GUI.HorizontalSlider (Rect (25, 113 ,100, 30), heightSliderValue, 5.0, 500.0);

var heightString = heightSliderValue.ToString();

GUI.Label (Rect (80, 95, 100, 30),heightString+" M");

Thanks!

If required its also possible to do this in reverse (float to string) too using ‘float.Parse’

var foo = "123.00";
var blah = float.Parse(foo);

(I had to convert a load of strings around before so I know how frustrating it can get! :))

2 Likes