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

  • Rounding, multiple decimals off and Locals

    I have been trying to get the round function to feed some locals that I use to create graphs labels, however, rounding has proven particularly challenging, as this actual example illustrates

    . qui centile variable if year==2007, c(10 25 40 50 60 75 90)

    . local pop=r(N)

    . count if variable==0 & year==2007

    . local zeros=r(N)

    . local ptile = 100*round(`zeros'/`pop',0.0001)

    . di "zero ptile=`ptile'"
    zero ptile=84.54000000000001

    . local ptile = round(100*`zeros'/`pop',0.0001)

    . di "zero ptile=`ptile'"
    zero ptile=84.54000000000001

    As you see, this is not nice rounding. When I try to use this function to label my graphs and other things I don''t know how to overcome this problem. Can anyone tell me how??

    Probably this has been solved before, but I have not being able to find how to solve this matter so I woudl appreciate if I am pointed the right forum too.

  • #2
    Welcome to Statalist.

    Review the output of help precision to get an understanding of why numbers like 84.54 cannot be stored precisely in Stata (or in any computer software using a base-2 system of representing floating point numbers).

    Luckily, it can be made to display in an attractive format.

    . local ptile = 84.54
    . di "zero ptile = `ptile'"
    zero ptile = 84.54000000000001
    . di "zero ptile =" `ptile'
    zero ptile =84.54
    . di "zero ptile =" %9.4f `ptile'
    zero ptile =  84.5400


    • #3
      As William Lisowski underlines round() with fractional arguments almost never can and never will do exactly what you want. In particular almost all multiples of 0.01 can't be held exactly in binary (the exceptions are just 0.25, 0.50 and 0.75 out of 0.01(0.01)0.99).

      As he underlines what you want is to be obtained by using a display format chosen for the purpose. gave a review of this and surrounding territory.


      • #4
        Thanks a lot! This solves the issue