Search Unity

Convert float to string

Discussion in 'Scripting' started by Thordis, Jan 20, 2007.

Thread Status:
Not open for further replies.
  1. Thordis

    Thordis

    Joined:
    Mar 1, 2006
    Posts:
    18
    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
     
    MMartin08 likes this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    totalWeight.ToString();
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  4. Thordis

    Thordis

    Joined:
    Mar 1, 2006
    Posts:
    18
    Ah! Thank you both very much!

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

    Cheers,
    Will
     
    TooManySugar likes this.
  5. DrHotbunz

    DrHotbunz

    Joined:
    Feb 14, 2009
    Posts:
    315
    Two years later. But thanks! I used the information.
     
    jasonasaad2 likes this.
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Er... three years? ;-) Even better...
     
  7. DrHotbunz

    DrHotbunz

    Joined:
    Feb 14, 2009
    Posts:
    315
    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.
     
  8. BlackHawkStudios

    BlackHawkStudios

    Joined:
    May 1, 2011
    Posts:
    107
    4 Years? I think I win :D
     
  9. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    You sad person.
     
  10. alexfeature

    alexfeature

    Joined:
    Apr 1, 2010
    Posts:
    132
    Necrophiliacs the lot of you :D
     
  11. BlackHawkStudios

    BlackHawkStudios

    Joined:
    May 1, 2011
    Posts:
    107
    I can't help the urge! Plus besides the obvious I kept forgetting the parenthesis.
     
  12. DavidDebnar

    DavidDebnar

    Joined:
    Mar 19, 2011
    Posts:
    115
  13. ehsan7715

    ehsan7715

    Joined:
    Apr 10, 2010
    Posts:
    2
    make fun with unity !! :D
     
  14. BazLevelUp

    BazLevelUp

    Joined:
    Jun 27, 2012
    Posts:
    36
  15. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
  16. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    34 years!
     
  17. bubbalovesponge

    bubbalovesponge

    Joined:
    Nov 8, 2012
    Posts:
    69
    LOL 5 years!!!
     
  18. keithsoulasa

    keithsoulasa

    Joined:
    Feb 15, 2012
    Posts:
    2,126
    ""+ totalWeight;

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

    Nijamyang

    Joined:
    Aug 8, 2012
    Posts:
    34
    finnaly 6 years :D
     
  20. DFLY

    DFLY

    Joined:
    Sep 18, 2012
    Posts:
    5
    6 years 3 months:D
     
  21. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    Why don't you mods make this thread a sticky hehe.
     
  22. bustedkrutch

    bustedkrutch

    Joined:
    Jun 24, 2013
    Posts:
    20
    6 years + 7 months + 4 days
    I win for today :)
     
  23. Reflexters

    Reflexters

    Joined:
    Sep 30, 2013
    Posts:
    1
    6 years + 8 months + 12 days
    I win!! :D
     
  24. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Oh my God !! I just found an ancient thread buried deep within the humongous heap of information.......Still helpful ;)
     
  25. MrSteve1

    MrSteve1

    Joined:
    Sep 27, 2012
    Posts:
    11
    7 Years. I win!
     
    furrilis and jasonasaad2 like this.
  26. thoxy96

    thoxy96

    Joined:
    Apr 7, 2014
    Posts:
    1
    7 years anyone?
     
Thread Status:
Not open for further replies.