# Re: st: Determining the width of a formatted value

 From wgould@stata.com (William Gould, Stata) To statalist@hsphsun2.harvard.edu Subject Re: st: Determining the width of a formatted value Date Mon, 07 Apr 2003 09:52:26 -0500

```Bryan Sayer <BSayer@s-3.com> 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

----+----1--
12,345,678

yet
. display %12.0gc 123456789

results in

----+----1--
123456789

when

----+----1--
123,456,789

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.

----+----1----+----2
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:

.0000314159
3.14159e-06

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

.0000031415

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:

----+----1----+----2
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:

3.1412e+05
3.1412e+07
3.1412e+09

or rounding and padding the numbers with zeros

314200
31420000
3142000000

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.
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
position.

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
wgould@stata.com
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```