Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Converting float to integer

Discussion in 'Extensions & OnGUI' started by iceherosubzero, Jul 30, 2009.

  1. iceherosubzero

    iceherosubzero

    Joined:
    Jul 10, 2009
    Posts:
    33
    how can i convert a float value to integer to display in gui.label. using cs. C#
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,598
    untested but should work:

    Code (csharp):
    1. int myBlubb = myFloatBlubb as int;
    or

    Code (csharp):
    1. int myBlubb = (int) myFloatBlubb;
    or just the right side when setting the label text.

    If you want to totally overcontrol it, you can also use String.Format ...
     
  3. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
  4. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    2,958
    I still got one ;-)

    Code (csharp):
    1. float myFloat = 3.5F;
    2. int myInt = Convert.ToInt32(myFloat);
    The difference between the casts ((int)myFloat vs. myFloat as int) *usually* is that using "as" will return null if there is no cast possible while "(int)" will throw an exception when no cast is possible. I don't know for sure how that behaves for ints for which there is no such thing as null as it's a value type - might be you get "0" instead when you use the "as" operator - or it throws some sort of (compiler-)error (haven't tested it myself, since the documentation of the as operator says it requires a reference type, I guess you will get an error).

    Some interesting references:

    Why Convert.ToInt32 Might be Dangerous
    Is casting the same thing as converting?
    Casting and Type Conversions


    Anyways, I'd use the Mathf-methods that Perlohmann suggested as they are easiest to read and understand (you can easily see what the code does by just reading the method names instead of having to know all the details of casting and converting).
     
  5. Corrosius

    Corrosius

    Joined:
    Nov 19, 2013
    Posts:
    1
    so this is VERY late answer but if someone is still looking for easiest way to accomplish this here:
    Code (csharp):
    1. floatVariable.ToString ("0")
    this displays only full numbers, if you put "0.0" instead it would display numbers with just one decimal etc...
     
  6. zhoufei

    zhoufei

    Joined:
    Jun 8, 2013
    Posts:
    91
    To complete on what Jashan said, using as with a non nullable value (int, float, etc...) will throw a compiler error, but using nullable type created from value type (int?, float?, etc...) will also throw a compiler error.
     
  7. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,039
    Well, at least you're necro'ing a timeless thread...

    Also, keep in mind that casting/converting will do something similar to rounding. Other options are to use floor and ceil.

    EDIT:
    Aaand that was already mentioned above...
     
  8. anmol77

    anmol77

    Joined:
    Apr 23, 2016
    Posts:
    2
    thanks for everything.....




    NOOR
     
  9. anmol77

    anmol77

    Joined:
    Apr 23, 2016
    Posts:
    2
  10. crispybeans

    crispybeans

    Joined:
    Apr 13, 2015
    Posts:
    210
    there is a fairly easy way to do it if you don't want to format the value :

    mylabel.text = "helloworld: "+floatvalue;

    In case you want to format it use something like this:
    lblDistanceToPhoto.text = image_distance.ToString("0.00");//2dp Number
     
  11. Jhonf3d

    Jhonf3d

    Joined:
    Apr 16, 2017
    Posts:
    1
    Thank you, bro!!!!!!
     
  12. Marc0101

    Marc0101

    Joined:
    Apr 20, 2017
    Posts:
    1
    In late! Thanks a lot!!! Very useful! ^_^
     
  13. kyawzawoo2088

    kyawzawoo2088

    Joined:
    May 24, 2018
    Posts:
    1
    thz buddy :)
     
  14. GallChi

    GallChi

    Joined:
    Jan 16, 2018
    Posts:
    3
    thanks :)