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

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))

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

-- with different prefactors as desired. 

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

. di floor(log10(123456789))

. di floor(log10(.123456789))

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. 


Miranda Kim

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

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")'"
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")'"
not show only 1 significant figure?

*   For searches and help try:

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