Announcement

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

  • Decimals

    Dear all,
    When variables take values with many decimal. How can I keep only the first 2?
    Thank you.

  • #2
    A very short answer is that you can't.

    The problem is an even shorter answer: precision.

    Code:
    search precision
    will turn up many resources.

    You want to record data as multiples of 0.01. But most multiples of 0.01 don't have exact binary representations, which is what computers use. Enormous skill and ingenuity go into relieving users of needing to worry, or even to think, about that, but sometimes it bites. Off the top of my head, I know that numbers ending in .50 do have exact binary representations, and numbers ending in .25 or .75, but that's it I think.

    What you can do, and this may be enough for your purposes, is to insist on a display format such as %3.2f.

    Comment


    • #3
      Off the top of my head, I know that numbers ending in .50 do have exact binary representations, and numbers ending in .25 or .75, but that's it I think.
      That's correct. The generalization is that an exact representation in base B is possible for fraction M/N if and only if all of the prime factors of N divide B. When B is 2, as in binary computers, this means that N must be a power of 2. Going out to two decimal places, only 1/4, 2/4, and 3/4 meet this criterion--hence Nick's result. The same generalization implies that an exact representation in base 10 is possible only for fractions whose denominators are products of powers of 2 and powers of 5.

      Comment


      • #4
        Thanks both for the answers.
        Already tried
        Code:
           format %3.2f  var1
        But the values of the specific variable did not change.
        Still have more than 2 decimals.
        Why is that?

        Comment


        • #5
          Changing the display format won't change the values. It just affects what is shown when the display format is respected.

          Show us the code and the output that underlie your statement.

          Comment


          • #6
            What about the calculations?They will be taken into account in the calculations?
            For example in the summation with other variables?

            Comment


            • #7
              Display format is what it says, display format. Stata doesn't pay any attention to display format in calculations.

              More at: http://www.stata-journal.com/sjpdf.h...iclenum=dm0067

              In a concrete and common case, currency calculations based on large unit/100 small units are best done as integer calculations and then presented as you wish.

              Comment


              • #8
                Ok I understood.
                Thank you very much.

                Comment

                Working...
                X