Statalist


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

RE: st: Keeping trailing zeros when formatting a decimal


From   Roy Wada <roywada@hotmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Keeping trailing zeros when formatting a decimal
Date   Thu, 1 Oct 2009 08:11:19 -0700

> Subject: st: Keeping trailing zeros when formatting a decimal

This is actually difficult to do. The concept of significant digits
was nonexistant in Stata until it was introduced by -outreg2-.
 
The following program will do the plus-1 significance, meaning it 
adds one more than you asked for.
 
I was going to put it up on ssc last year but never got around to it.
 
Roy
 

autofmt, input(2.200) auto(2)
ret list
 
 
*! autofmt 0.9 by roywada@hotmail.com
* Aug2008
* getting the significant digits
program define autofmt, rclass
version 7.0
* valstr renamed as fmt
syntax, input(string) [auto(integer 3) less(integer 0) parse(string)]
* parse( ) takes only one character; " " is always included as a parse
if `"`parse'"'=="" {
        local parse " "
}
local rest `"`input'"'
local count 0
if "`rest'"~="" {
        * handles the possibility the first token is empty
        gettoken first rest: rest, parse("`parse'")
        local first=trim(`"`first'"')
        if `"`first'"'==`"`parse'"' {
                local count=`count'+1
                local input`count' ""
        }
        else {
                local count=`count'+1
                local input`count' `"`first'"'
        }
}
while "`rest'"~="" {
        gettoken first rest: rest, parse("`parse'")
        local first=trim(`"`first'"')
        if `"`first'"'~=`"`parse'"' {
                local count=`count'+1
                local input`count' `"`first'"'
        }
}

if `count'==0 {
        * input( ) was left empty
        exit
}

*** run as many times
forval num=1/`count' {
        * confirm a number
        capture confirm number `input`num''
        local rc=_rc
        
        * run if not missing and is a number
        if "`input`num''"~="." & "`input`num''"~="" & `rc'==0 {
                local times=0
                local left=0
                
                * integer checked by modified mod function
                if round((`input`num'' - int(`input`num'')),0.0000000001)==0 {
                        local whole=1
                }
                else {
                        local whole=0
                        * non-interger
                         if `input`num''<. {
                                
                                * digits that need to be moved if it were only decimals: take the ceiling of log 10 of absolute value of decimals
                                local times=abs(int(ln(abs(`input`num''-int(`input`num'')))/ln(10)-1))  
                                
                                * the whole number: take the ceiling of log 10 of absolute value
                                local left=int(ln(abs(`input`num''))/ln(10)+1)
                        }
                }
                
                
                * assign the fixed decimal values into aadec
                if `whole'==1 {
                        local aadec=0
                }
                else if .>`left' & `left'>0 {
                        * reduce the left by one if more than zero to accept one extra digit
                        if `left'<=`auto' {
                                local aadec=`auto'-`left'+1
                        }
                        else {
                                local aadec=0
                        }
                }
                else {
                        local aadec=`times'+`auto'-1
                }
                
                if "`less'"=="" {
                        * needs to between 0 and 11
                        if `aadec'<0 {
                                local aadec=0
                        }
                        *if `aadec'<11 {
                        if `aadec'<7 {
                                * use fixed
                                local fmt "`aadec'f"
                        }
                        else {
                                * use exponential
                                local fmt "`=`auto'-1'e"
                        }
                }
                else {
                        * needs to between 0 and 11
                        local aadec=`aadec'-`less'
                        if `aadec'<0 {
                                local aadec=0
                        }
                        *if `aadec'<10 {
                        if `aadec'<7 {
                                * use fixed
                                local fmt "`aadec'f"
                        }
                        else {
                                * use exponential
                                local fmt "`=`auto'-1'e"
                        }
                }
                
                * make it exponential if too big
                if `input`num''>1000000 & `input`num''<. {
                        local fmt "`=`auto'-0'e"                
                }
                
                * make it exponential if too negative (small)
                if `input`num''<-1000000 & `input`num''<. {
                        local fmt "`=`auto'-0'e"                
                }
                
                local fmt`num' `fmt'
                local aadec`num' `aadec'
                
                
                local output`num'=string(`input`num'',"%12.`fmt'")
                
                return scalar deci`num'=`aadec'
                return local fmt`num'="`fmt'"
                return local input`num'="`input`num''"
                
                return local output`num'=`"`output`num''"'
        }
        else {
                * it is a missing value, empty, or non-number
                local output`num'=trim(`"`input`num''"')
                
                return scalar deci`num'=.
                return local fmt`num'="."
                if "`input`num''"=="" {
                        * return a dot when empty
                        return local input`num'="."
                }
                else {
                        return local input`num'="`input`num''"
                }
                
                return local output`num'=`"`output`num''"'
        }
}
end

 
 
  		 	   		  
_________________________________________________________________
Lauren found her dream laptop. Find the PC that’s right for you.
http://www.microsoft.com/windows/choosepc/?ocid=ftp_val_wl_290
*
*   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/



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