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

 From Roy Wada To Subject RE: st: RE: Keeping trailing zeros when formatting a decimal Date Thu, 1 Oct 2009 13:30:07 -0700

Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

=20
Here is a repost with something added:
=20
log10 wasn't available back then. I am not sure floor( ) was available=2C e=
ither.

At least that's my excuse.
=20
In a way my codes are utterly basic=2C doing it from scratch from the princ=
iples
of high school mathematics.

No doubt the codes can be shortened=2C but you need to count the whole numb=
ers=20
to the left of a decimal as a part of signficant digits.
=20
=20
=20
local input 123.12345000
local dec 5
=20
local decimals=3Dabs(int(log10(`input')))
local whole=3Dint(log10(`input'))
if `whole'<=3D`dec' {
local format=3D`dec'-`whole'
}
else {
local format=3D0
}
di "`=3Dstring(`input'=2C"%12.`format'f")'"
=20
=20
=20
Incidently this is an example that doesn't work without a further adjustmen=
t.

. di round(10 * 10^mod(log10(0.0001)=3D2C 5))
100

Roy

=20
> This generated a lively thread. (Oddly enough=2C although I still remembe=
r
> 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=
=2C
> 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 		 	   		  =0A=
_________________________________________________________________=0A=
Bing=99  brings you maps=2C menus=2C and reviews organized in one place.   =
Try it now.=0A=
http://www.bing.com/search?q=3Drestaurants&form=3DMLOGEN&publ=3DWLHMTAG&cre=
a=3DTEXT_MLOGEN_Core_tagline_local_1x1=
*
*   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/