Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • int() and round()

    I am rounding values of a variable to the closest integer.
    For some reason, int(x) and round(x) give me two slightly different numbers.
    The correlation between the variable rounded with int() and the same variable but rounded with round(x) is about 0.95.
    Is there some minimal difference in the way Stata computes the integers?

  • #2
    See e.g. the help on these functions.

    Code:
    help int()
    int() rounds towards zero. So int(2.9) and int(-2.9) yield 2 and -2 respectively.

    In contrast round() rounds to the nearest integer with no second argument. So, round(2.9) and round(-2.9) yield 3 and -3 respectively.

    More also at https://www.stata-journal.com/articl...article=dm0095

    Comment


    • #3
      Thank you!

      Comment


      • #4
        One of the details I uncovered in writing up dm0095 was that int() or INT() doesn't mean the same in all software. INT() in MS Excel always rounds down.


        Otherwise put, watch out for rounding error!

        Comment

        Working...
        X