Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: Re: Error with -round()-


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: Error with -round()-
Date   Sun, 9 Mar 2014 15:10:39 +0900

Matt Vivier wrote:

I'm using:
Stata/IC 12.1 for Windows (64-bit x86-64)
Revision 06 Feb 2012

I'm very hesitant to suggest that there is a bug in Stata itself, but
I am getting an unexpected result when using -round()- on some
numbers, and have been unable to find an explanation for it.

I'm pulling information out of my dataset, and writing it to a table
in an HTML file, and wanted to round the values down to something of
reasonable length. The vast majority of the time, -round()- functions
as expected, but it occasionally seems to misfire.

This can be replicated with:
local value = round(.8125,.01)
di "`value'"

However, if you type
di `value'

You see the expected result.
Unfortunately, (I believe) I have to enclose it in quotes to write it
to the HTML file as such:
file write html `"<TD>`value'</TD>"'

My questions are:
Can anyone replicate this issue?
If so, any thoughts on what causes it? It seems to be caught up in
treating `value' like a string due to the quotes.
Any thoughts on a workaround?

--------------------------------------------------------------------------------

You can try something like that below for writing the text to file when you want
to display floats or doubles in a fixed-decimal format. 

Joseph Coveney

. version 12.1

. 
. clear *

. set more off

. 
. tempfile tmpfil0

. tempname file_handle

. 
. file open `file_handle' using `tmpfil0', write text

. file write `file_handle' "<TD>" %04.2f (.8125) "</TD>" _newline(1)

. file close `file_handle'

. 
. type `tmpfil0'
<TD>0.81</TD>

. 
. exit

end of do-file

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index