Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: rounding in STATA

From   "Nick Cox" <>
To   <>
Subject   st: RE: rounding in STATA
Date   Thu, 13 Apr 2006 14:37:02 +0100

I can't speak about STATA. In Stata, which behaves
similarly in this instance, you can get rounding up
by using -ceil()-. 

But note that you will find it difficult to avoid
small (apparent) anomalies whatever you do. 

This because, in general, multiples of 0.1 cannot 
be held exactly in binary, as explained many, many
times on this list. 

Suppose I have 8.75 (meaning, precisely, 8 + 3/4). 

Stata can hold that, _exactly_. In decimal, 

. di %23.18f 8.750 

or, more to the point, in hexadecimal, 

. di %21x 8.750 

If I round that, Stata starts to struggle: 

. di %21x round(8.750, 0.1)

. di %23.18f round(8.75,0.1)

This raises the question of what, exactly, is
the number you are showing us which is (which 
is represented as!) 8.750. Your number is, 
I surmise, not exactly 8.750, as if it were
it would round to 8.8. 

That said, there is no way you can avoid this kind of 
problem by writing your own program, unless 
you also construct a decimal-based computer. 


Siyam, Amani
> I have a minor issue with the way STATA is rounding, for example a
> variable to one decimal place.
> I used the command
> gen x=round(y, 0.1)
> Comparing the two variables, I noticed the that when:
>    x=			  y=
>  8.750		 8.7
>  5.752		 5.8
> 23.256		23.3
> Is there a way I can modify the function to round "x"=8.750 to be 8.8
> Or should I put together my own code to get the rounding I want.

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index