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

 From Roy Wada To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Keeping trailing zeros when formatting a decimal Date Thu, 1 Oct 2009 13:52:32 -0700

```Sorry about the mess. The server apparently doesn't like Hotmail.

This is a repost using Gmail:

log10 wasn't available back then. I am not sure floor( ) was
available, either. At least that's
my excuse. In a way my codes are utterly basic doing it from scratch
from the principles of
high school mathematics.

The codes can be shortened but you need to count the whole numbers to
the left of the decimal
point as a part of signficant digits.

local input 123.12345000
local dec 5

local decimals=abs(int(log10(`input')))
local whole=int(log10(`input'))
if `whole'<=`dec' {
local format=`dec'-`whole'
}
else {
local format=0
}

di "`=string(`input',"%12.`format'fc")'"

Incidently this is an example that doesn't work without a further adjustment.

. di round(10 * 10^mod(log10(.000001), 5))
100000

Roy

Nick said:

This generated a lively thread. (Oddly enough=2C 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)=2C 1))
12

. di round(10 * 10^mod(log10(123456789)=2C 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=2C 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=2C 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
*
*   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/
```