# st: RE: Keeping trailing zeros when formatting a decimal

 From "Nick Cox" To Subject st: RE: Keeping trailing zeros when formatting a decimal Date Thu, 1 Oct 2009 19:00:09 +0100

```This generated a lively thread. (Oddly enough, although I still remember
teachers stressing the idea of sig.fig. -- as it was called where I grew
up -- in practice I always want to program display with either Stata's
defaults or a fixed number of decimal places.)

My one contribution is slightly oblique. If existing formats don't suit,
it seems a better strategy to drill down and try first principles.

1. Significant figures can be got from the fractional part of the base
10 logarithm of whatever it is.

. di round(10 * 10^mod(log10(.123456789), 1))
12

. di round(10 * 10^mod(log10(123456789), 1))
12

-- with different prefactors as desired.

2. Multiplying powers can be got from the integer part of same.

. di floor(log10(123456789))
8

. di floor(log10(.123456789))
-1

3. Logically it seems that's all the information you need to work out
leading and trailing zeros, except that if there's a negative sign you
have to strip it off first and put it back again.

I haven't tried to understand Roy Wada's program, which at heart should
be equivalent to this. No doubt the help file will document that it also
deals with complications that I have missed.

Nick
n.j.cox@durham.ac.uk

Miranda Kim

I would be very grateful for advice on the following basic formatting
questions...

To store a number as a string with a format showing 2 significant
figures, I do the following, for example:
di "`=string(-0.000029, "%9.2g")'"

If the second significant figure is a zero, how can I make sure this is
still displayed?
The following produces:
. di "`=string(-0.0000201, "%9.2g")'"
-.00002
when I want it to display "-0.000020"

Also, how can I make sure it displays the zero before the decimal point?

Also, why does
. di "`=string(-0.000029, "%9.1g")'"
-.000029
not show only 1 significant figure?

*
*   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/
```