Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Determining the width of a formatted value

From   [email protected] (William Gould, Stata)
To   [email protected]
Subject   Re: st: Determining the width of a formatted value
Date   Mon, 07 Apr 2003 09:52:26 -0500

Bryan Sayer <[email protected]> asked about how the width <w> is treated in the
%<w>.<d>g and %<w>.<d>gc display formats.  For instance, 

        . display %12.0gc 12345678

results in 


	. display %12.0gc 123456789

results in 




looks possible to Bryan.  The answer is that %g and %gc puts aside two of the
positions for a sign and for a decimal point in case either should be
necessary.  By this logic, a %12.0gc format has 12-2=10 positions for digits
and commas.  Obviously, the code could be written to make use of the positions
for the sign and decimal point when they are not necessary, but setting aside
the two positions is how the code works right now and it is not on our list 
to change that.

Another subject

What follows is unrelated to Bryan's query.

I would like to mention the new-to-Stata-8 %<w>.<d>g and %<w>.<d>gc formats
when <d> is greater than 0.  I am not certain many of you noticed the change.

%12.0g, as you know, shows a number in field of width 12.  That number is
displayed in such away as to show as many of its digits as possible.

         3.141592654          <- display %12.0g _pi
         31.41592654          <- display %12.0g _pi*10
         314159.2654          <- display %12.0g _pi*10^5
         314159265.4          <- display %12.0g _pi*10^8
          3141592654          <- display %12.0g _pi*10^9
         3.14159e+10          <- display %12.0g _pi*10^10

         3.141592654          <- display %12.0g _pi
         .3141592654          <- display %12.0g _pi*10^(-1)
         .0003141593          <- display %12.0g _pi*10^(-4)
         .0000314159          <- display %12.0g _pi*10^(-5)
         3.14159e-06          <- display %12.0g _pi*10^(-6)

I want to draw you attention especially to that last two lines:


Note how smart is %12.0g.  It could have displayed _pi*10^(-6) as


but it knew that, if it switched to %e format, it could display an extra
digit, and so it did.  That's the purpose of the %g format:  to show as many
digits as possible within the specified width.

New to Stata 8, you can specify the maximum number of digits you want, which 
you specify as the <d> in %<w>.<d>g:

               3.142          <- display %12.4g _pi
               31.42          <- display %12.4g _pi*10
                3142          <- display %12.4g _pi*10^3
              314159          <- display %12.4g _pi*10^5 
            31415927          <- display %12.4g _pi*10^7
          3141592654          <- display %12.4g _pi*10^9
           3.142e+10          <- display %12.4g _pi*10^10

               3.142          <- display %12.4g _pi
               .3142          <- display %12.4g _pi*10^(-1)
            .0003142          <- display %12.4g _pi*10^(-4)
           .00003142          <- display %12.4g _pi*10^(-5)
          3.1412e-06          <- display %12.4g _pi*10^(-6)

%12.4g is almost a 4-significant digit format, the exceptions being 

              314159          <- display %12.4g _pi*10^5 
            31415927          <- display %12.4g _pi*10^7 
          3141592654          <- display %12.4g _pi*10^9

To show those with 4 digits would have required either going to %e format:


or rounding and padding the numbers with zeros


The %w.dg, d>0, format assumes you do not want either of those solutions.
If you wanted the %e format, you can use the %e format at the outset.
We thought about implementing the round-and-pad solution, but came to the 
conclusion that researchers don't want that.  As long as digits are 
going to be displayed, one might as well put the actual digit in each 

If there is an interest in the round-and-pad solution, we would be willing to
implement a %G format to go along with %g.

-- Bill
[email protected]
*   For searches and help try:

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