Search Unity

Converting float to integer

Discussion in 'Immediate Mode GUI (IMGUI)' started by iceherosubzero, Jul 30, 2009.

  1. iceherosubzero

    iceherosubzero

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

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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:
    3,307
    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).
     
    HalilHalid, AlyssaFaden and Edisyo like this.
  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. tgouala-wellfiredLtd

    tgouala-wellfiredLtd

    Joined:
    Jun 8, 2013
    Posts:
    99
    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,513
    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!!!!!!
     
    hazzapc123 likes this.
  10. Marc0101

    Marc0101

    Joined:
    Apr 20, 2017
    Posts:
    7
    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:
    4
    thanks :)
     
  13. j04milan

    j04milan

    Joined:
    Feb 6, 2019
    Posts:
    44

    Thanks BRO
     
  14. LastAustralian

    LastAustralian

    Joined:
    Dec 10, 2018
    Posts:
    1
    5 Years later and Thankyou this was helpful, just solved my problem :)
     
    sunny15grover likes this.
  15. Infinite-3D

    Infinite-3D

    Joined:
    Jan 5, 2020
    Posts:
    39
    use Mathf.RoundToInt
     
    Jroel, vorion, FunniDino and 3 others like this.
  16. WilliamHerring

    WilliamHerring

    Joined:
    Aug 29, 2020
    Posts:
    4
    Another way to do this is convert it to string and then to an int like so:
    Code (CSharp):
    1. float i = 1.2f;
    2.  
    3. int newInt = Parse(i.ToString("0"));
     
  17. ma930213

    ma930213

    Joined:
    Oct 18, 2019
    Posts:
    1
    Use @Dreamora Method, i.e. (int) float_value.
    It worked for me.
     
  18. Raniolovonmerlo

    Raniolovonmerlo

    Joined:
    Mar 30, 2021
    Posts:
    1
    public float xxx;
    public TextMeshProUGUI textxx;


    textxx.text = ((int)xxx).ToString();


    this show INT .
     
  19. virgilcwyile

    virgilcwyile

    Joined:
    Jan 31, 2016
    Posts:
    73
    Casting using (int) is the worst way. It would give wrong values. I casted 0.3/0.1 to Int. It should be 3 but it gives 2.