[R] Displaying trailing zeroes

Duncan Murdoch murdoch at stats.uwo.ca
Thu Jan 24 23:51:22 CET 2008


On 24/01/2008 4:36 PM, Lucke, Joseph F wrote:
> round(12.01,1) will give the answer 12, not 12.0 or even 12.  

Those are all the same number.  You aren't asking about the answer, you 
are asking about how to control how the number is printed.

To make a
> table look nice, I need to display the trailing zero so that just as
> round(12.05,1) yields 12.1, round(12.01) yields 12.0. I cannot find an
> answer in print() or format() or options().  Any suggestions would be
> appreciated.

R tries to be consistent when it prints a vector, so you could convert 
all the entries at once.  For example,

 > x <- c(12, 12.1)
 > format(x)
[1] "12.0" "12.1"

If you want individual control on each entry, see formatC or sprintf. 
Christos gave you the formatC version; the sprintf version is

 > sprintf("%.1f", 12)
[1] "12.0"

Duncan Murdoch

> Joseph F. Lucke, PhD
> Biostatistician
> Center for Clinical Research and Evidence-based Medicine
> University of Texas Medical School at Houston
> Email: Joseph.F.Lucke at uth.tmc.edu
>  
> 
> ______________________________________________
> 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.



More information about the R-help mailing list