Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. 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
    Code (csharp):
    1.  
    2.  
    3. float fValue = 0.123456f;
    4. int iValue = (int)fValue;
    5. Debug.Log("int val: " +iValue);
    6.  
    7. iValue = Mathf.FloorToInt(fValue);
    8. Debug.Log("int val: " +iValue);
    9.  
    10. iValue = Mathf.CeilToInt(fValue);
    11. Debug.Log("int val: " +iValue);
    12.  
    13. iValue = Mathf.RoundToInt(fValue);
    14. Debug.Log("int val: " +iValue);
    15.  
    16.  
    And there are most likely more ways.

    //Perlohmann
     
  4. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    2,977
    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...
     
    aaro4130, Jhonf3d, OR_Parga and 2 others like this.
  6. tgouala-wellfiredLtd

    tgouala-wellfiredLtd

    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,047
    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. 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
     
  9. Jhonf3d

    Jhonf3d

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

    Marc0101

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

    kyawzawoo2088

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

    GallChi

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