Hi,> a <- 4> a*0.2[1] 0.8 ok!! Is there a method to obtain this:> a*0.2[1] 0.80 I need to round the number also with the zero. Thanks in advance, Alfredo
On 11-03-26 2:05 PM, Alfredo Alessandrini wrote:> Hi, > >> a<- 4 > >> a*0.2 > [1] 0.8 > > ok!! > > Is there a method to obtain this: > >> a*0.2 > [1] 0.80It is easy to print values with trailing zeros, but I think it's hard to make it the default. The sprintf() function is the one I'd use for the formatting: sprintf("%.2f", a*0.2) (or put noquote() around it if you don't like the quoted result). Duncan Murdoch> > I need to round the number also with the zero. > > > > Thanks in advance, > > Alfredo > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Alfredo, Try noquote(sprintf("%.2f", a*.2)) HTH, Jorge On Sat, Mar 26, 2011 at 2:05 PM, Alfredo Alessandrini <> wrote:> Hi, > > > a <- 4 > > > a*0.2 > [1] 0.8 > > ok!! > > Is there a method to obtain this: > > > a*0.2 > [1] 0.80 > > I need to round the number also with the zero. > > > > Thanks in advance, > > Alfredo > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Sat, Mar 26, 2011 at 07:05:40PM +0100, Alfredo Alessandrini wrote:> Hi, > > > a <- 4 > > > a*0.2 > [1] 0.8 > > ok!! > > Is there a method to obtain this: > > > a*0.2 > [1] 0.80 > > I need to round the number also with the zero.Hi. Try the following formatC(a*0.2, digits=2, format="f") [1] "0.80" noquote(formatC(a*0.2, digits=2, format="f")) [1] 0.80 The two-digit form 0.80 is used also if a vector is printed and some other component requires 2 digits. For example c(a*0.2, 0.01) [1] 0.80 0.01 Hope this helps. Petr Savicky.