Convert float to string

Hey all, I’m sure this is common and simple, but I couldn’t find it in the docs…
How do I convert a float to a string.

i.e. the following does not work:

//
var weightfield = GameObject.Find(“WeightField”).GetComponent(“TextMesh”);
weightfield.text = totalWeight;
//

However, if I change it to weightfield.text = “hello”, then hello is displayed.

Thanks for any help!
Cheers,
Will

2 Likes

totalWeight.ToString();

8 Likes

Also, you can give it a format string if you want more control over the output. Say, if you want it to only show two digits after the decimal: .ToString(“#.00”);

I don’t know all the options for this, but it’s pretty powerful.

6 Likes

Ah! Thank you both very much!

Heh I tried ToString initially but my stupid self forgot the parenthesis.

Cheers,
Will

1 Like

Er… three years? :wink: Even better…

my search page is set to this.

Its the best way to search everything in unity at one time. Super resource! That way I often find things like this thread that can help me.

You sad person.

Necrophiliacs the lot of you :smile:

I can’t help the urge! Plus besides the obvious I kept forgetting the parenthesis.

make fun with unity !! :smile:

A link which explain how to use the option of ToString() could be usefull :wink:

Here you are : SteveX Compiled » String Formatting in C#

to up your ante baz, the MSDN documentation on formatting types:

which has break down links to all the sub data type formats (see bottom in ‘related articles’)

“”+ totalWeight;

Adding a string even an empty one to a float or int will convert it

Why don’t you mods make this thread a sticky hehe.

Oh my God !! I just found an ancient thread buried deep within the humongous heap of information…Still helpful :wink:

Closed to prevent unnecessary bumping/spam to an answered topic.