Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: Problems rounding a variable using round()

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Problems rounding a variable using round() Date Sun, 25 Mar 2012 09:16:04 +0100

```In addition,

. search precision

and read around on the subject.

-round()- cannot make your fraction have exact values of 0.2, 0.4,
0.6, 0.8 as none of those exact decimals have an exact binary
equivalent. 0 and 1 don't pose this small problem.

More positively, this is not really a problem, as all you need is to
use a -format- that ensures display of results as you want it.

Nick

On Sun, Mar 25, 2012 at 5:57 AM, Kieran McCaul <kieran.mccaul@uwa.edu.au> wrote:

> This is a precision problem. Try:
> count if y == float(0.6)

L S

> I am creating a variable that is the average of 5 0/1 variables.
> However, instead of taking values on {0,.2,.4.6,.8,1}, it is taking
> values like .40000001 and .60000002.  I tried to use the round()
> command as below, but it's not working.  Any thoughts on how I can
> make y take on the values {0,.2,.4.6,.8,1}?
>
> Thank you.
>
> clear
> set obs 1000
>
> set seed 1234
>
> forvalues i=1/5{
>        gen x`i'=(uniform()>.5)
> }
>
> gen y = (1/5)*(x1+x2+x3+x4+x5)
> * browse y
> count if y==.6
>
> replace y= round(y,.1)
> count if y==.6

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```