# Convert float to string

**URL:** https://discussions.unity.com/t/convert-float-to-string/375934
**Category:** Scripting
**Tags:** Scripting
**Created:** [January 20, 2007, 12:05am UTC](https://discussions.unity.com/t/convert-float-to-string/375934 "2007-01-20T00:05:25Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_62887450](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon\_62887450](https://discussions.unity.com/u/anon_62887450)
#### Post date: [January 20, 2007, 12:05am UTC](https://discussions.unity.com/t/convert-float-to-string/375934/1 "2007-01-20T00:05:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Joachim\_Ante\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joachim_ante_1/32/44155_2.png) [@Joachim\_Ante\_1](https://discussions.unity.com/u/Joachim_Ante_1)
#### Post date: [January 20, 2007, 12:34am UTC](https://discussions.unity.com/t/convert-float-to-string/375934/2 "2007-01-20T00:34:17Z")

</div>

totalWeight.ToString();

---

<div class="post-metadata">

### Author: ![StarManta](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/starmanta/32/280327_2.png) [@StarManta](https://discussions.unity.com/u/StarManta)
#### Post date: [January 20, 2007, 2:26pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/3 "2007-01-20T14:26:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![anon\_62887450](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon\_62887450](https://discussions.unity.com/u/anon_62887450)
#### Post date: [January 21, 2007, 6:05am UTC](https://discussions.unity.com/t/convert-float-to-string/375934/4 "2007-01-21T06:05:57Z")

</div>

Ah! Thank you both very much!

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

Cheers,  
Will

---

<div class="post-metadata">

### Author: ![andeeeee](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@andeeeee](https://discussions.unity.com/u/andeeeee)
#### Post date: [January 14, 2010, 8:51pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/6 "2010-01-14T20:51:10Z")

</div>

> [@""](#):
>
> Two years later. But thanks! I used the information.

Er… _three_ years? 😉 Even better…

---

<div class="post-metadata">

### Author: ![anon\_89678146](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@anon\_89678146](https://discussions.unity.com/u/anon_89678146)
#### Post date: [January 15, 2010, 1:13am UTC](https://discussions.unity.com/t/convert-float-to-string/375934/7 "2010-01-15T01:13:39Z")

</div>

my search page is set to [this](http://www.google.com/#hl=en&source=hp&q=site%3AUnity3d.com&aq=f&aqi=&oq=&fp=e8d6ef47431c6a4a).

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.

---

<div class="post-metadata">

### Author: ![BlankBlank5454](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@BlankBlank5454](https://discussions.unity.com/u/BlankBlank5454)
#### Post date: [May 21, 2011, 10:13pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/9 "2011-05-21T22:13:14Z")

</div>

You sad person.

---

<div class="post-metadata">

### Author: ![alexfeature](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexfeature/32/360981_2.png) [@alexfeature](https://discussions.unity.com/u/alexfeature)
#### Post date: [May 21, 2011, 10:23pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/10 "2011-05-21T22:23:39Z")

</div>

Necrophiliacs the lot of you 😄

---

<div class="post-metadata">

### Author: ![anon\_54796857](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_54796857](https://discussions.unity.com/u/anon_54796857)
#### Post date: [May 21, 2011, 10:28pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/11 "2011-05-21T22:28:15Z")

</div>

> [@HolBol](#):
>
> You sad person.

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

---

<div class="post-metadata">

### Author: ![ehsan7715](https://avatars.discourse-cdn.com/v4/letter/e/8dc957/32.png) [@ehsan7715](https://discussions.unity.com/u/ehsan7715)
#### Post date: [July 26, 2012, 11:23am UTC](https://discussions.unity.com/t/convert-float-to-string/375934/13 "2012-07-26T11:23:30Z")

</div>

make fun with unity !! 😄

---

<div class="post-metadata">

### Author: ![BazLevelUp](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@BazLevelUp](https://discussions.unity.com/u/BazLevelUp)
#### Post date: [September 13, 2012, 3:29pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/14 "2012-09-13T15:29:46Z")

</div>

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

Here you are : [SteveX Compiled » String Formatting in C#](http://blog.stevex.net/string-formatting-in-csharp/)

---

<div class="post-metadata">

### Author: ![lordofduct](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/lordofduct/32/890751_2.png) [@lordofduct](https://discussions.unity.com/u/lordofduct)
#### Post date: [September 13, 2012, 3:37pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/15 "2012-09-13T15:37:06Z")

</div>

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

> **[Overview: How to format numbers, dates, enums, and other types in .NET - .NET](https://learn.microsoft.com/en-us/dotnet/standard/base-types/formatting-types?redirectedfrom=MSDN)**
>
> Learn how to convert instances of .NET types to formatted strings. Override the ToString method, make formatting culture-sensitive, and use ICustomFormatter.

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

---

<div class="post-metadata">

### Author: ![keithsoulasa](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@keithsoulasa](https://discussions.unity.com/u/keithsoulasa)
#### Post date: [November 8, 2012, 11:46pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/18 "2012-11-08T23:46:06Z")

</div>

“”+ totalWeight;

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

---

<div class="post-metadata">

### Author: ![willemsenzo](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/willemsenzo/32/537442_2.png) [@willemsenzo](https://discussions.unity.com/u/willemsenzo)
#### Post date: [April 9, 2013, 11:53am UTC](https://discussions.unity.com/t/convert-float-to-string/375934/21 "2013-04-09T11:53:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![wolfstien786](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@wolfstien786](https://discussions.unity.com/u/wolfstien786)
#### Post date: [October 3, 2013, 6:37am UTC](https://discussions.unity.com/t/convert-float-to-string/375934/24 "2013-10-03T06:37:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![vertxxyz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/vertxxyz/32/1011296_2.png) [@vertxxyz](https://discussions.unity.com/u/vertxxyz)
#### Post date: [August 31, 2024, 10:56pm UTC](https://discussions.unity.com/t/convert-float-to-string/375934/29 "2024-08-31T22:56:48Z")

</div>

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