Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: rounding error

From   Maarten Buis <>
Subject   Re: st: rounding error
Date   Tue, 20 Dec 2011 15:32:46 +0100

On Tue, Dec 20, 2011 at 2:57 PM, Richard Fox <> wrote:
> The results are generally as expected but sometimes the rounding does not seem to work.
> I capture the information I need as in the following example (this is just a small extract from the program, obviously there are argument definitions and tabulations etc.);
> local acc = .1
> qui su `x' if New`v' == Tnam[`i',1] & New`w'==Wnam`i'[`j',1],d
> local Median_`i'_`j' = round(r(p50),`acc')
> The results I obtain (see 2 examples below) often have values that have many decimal places which appears to suggest that the rounding is not working. However if I run a basic summary of the particular variable the results I get do not have the numerous decimal places so I'm not sure if the issue is with the rounding statement. Any ideas?

The problem is that computers store numbers in binary, and a number
like 84.3 in binary is like 1/3 in decimal; you cannot store it in a
finite number of digits. There are nice discussions on this topic at
the Stata blog:

There has also been a nice article in the Stata Journal:
William Gould (2006) Mata Matters: Precision. The Stata Journal. 6(4):550-560.

One solution is to realize that macros contain strings, and a computer
has no trouble storing the string "84.3", even though it cannot
exactly store the numer that string represents. Here the extended
macro function -display- can be helpful, for more see -help
extended_fcn- (*). So if you want to "round" a number like 84.34 to to
84.3 and store it as a string you type:

local rounded: display %3.1f 84.34

Hope this helps,

(*) In practice I always type -help macro- and than click on the link
to extended macro functions.

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index