Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Round vs RoundToInt

Discussion in 'Scripting' started by Screenhog, Dec 7, 2011.

  1. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
    Question: what's the difference between Mathf.Round and Mathf.RoundToInt? (Or CeilToInt or FloorToInt, for that matter.)

    I would have expected one to result in a float, and the other to result in an int. What's the difference?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're correct - that is the difference. :)
     
  3. Screenhog

    Screenhog

    Joined:
    Jul 2, 2009
    Posts:
    498
    But I did a test with Debug.Log... Mathf.Round(10.7) came up as 10 instead of 10.0. Should there not have been a decimal?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    No, for the same reason that
    Code (csharp):
    1.  
    2. public float foo = 10f;
    3.  
    is perfectly legal.
     
  5. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Yes same problem here...

    Why does the Unity's Round function return INT but the usual Round function is used as the following?


    a = Math.Round (1.234 ,2);
    >>> a = 1.23

    And Unity's one :

    a = Mathf.Round (1.234f);
    a = 1;

    Is there an existing function into Unity working like the first example?

    - http://msdn.microsoft.com/en-us/library/system.math.round(v=vs.71).aspx


    EDIT :

    Also reading that :
    - http://answers.unity3d.com/questions/50391/how-to-round-a-float-to-2-dp.html

    Code (csharp):
    1. yourFloat = Mathf.Round(yourFloat * 100f) / 100f;
    LOL ??? o_O??
     
    Last edited: Dec 9, 2011
  6. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Since you asked pointed to this thread from that other one which (went off-topic badly), I thought I may post the relevant parts from the other thread there, as others may be interested in it too.

    I'm not sure what your problem was over there. Documentation clearly states that Mathf.Round and Mathf.RoundToInt both methods round to the next integer. The only difference is, that Mathf.Round returns an float type value and Mathf.RoundToInt returns an integer type value, which saves you 1 casting and compiler warning, when working with strong typing.

    Documentation clearly states that Mathf.Round(...) is equivalent to Math.Round(..., 0); in .NET Framework.

    So if you want to round to 2 decimals, just use Math.Round(..., x) (the Mono/.NET implementation of Round).

    However, Math.Round in Mono/.NET only takes returns decimals () and doubles, so if you use them you have to cast back to float

    Code (csharp):
    1.  
    2. float percent = 23.239f;
    3. percent = (float)Math.Round(percent, 2);
    4.  
    since most values used in Unity (i.e. Vectors etc) are floats. If you want to complain, complain to Unity why they only implemented round to integers.
     
    Hraesvelgr-tc likes this.