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

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

From   "Nick Cox" <>
To   <>
Subject   RE: st: RE: Keeping trailing zeros when formatting a decimal
Date   Fri, 2 Oct 2009 18:42:33 +0100

Thanks for your comments. I look forward to seeing documentation of your

Your answer raises a tiny historical question. Looking at various
manuals I see that -log10()- was added in Stata 4 and -floor()- was
added in Stata 8, so neither can be regarded as recent additions to


Roy Wada

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

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

Nick said:

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

. di round(10 * 10^mod(log10(123456789)=2C 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=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
be equivalent to this. No doubt the help file will document that it also
deals with complications that I have missed.

*   For searches and help try:

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