[R] round function seems to produce maximum 2 decimals

Glenn E Stauffer gestauffer at gmail.com
Wed May 20 22:10:15 CEST 2009


I am trying to use round()to force R to display a specific number of
decimals, but it seems to display <=2 decimals no matter what I specify in
the digits argument. As an alternative I tried signif(), but it also
produces unexpected results. See example code and results below. Format()
works, but then the result no longer is numeric. Am I missing something
simple?
I am using R 2.9.0 on Windows XP. 
Thanks,
Glenn

#code
h=12345.16711
h

round(h,digits=1)
round(h,digits=2)
round(h,digits=3)
round(h,digits=4)
round(h,digits=5)

signif(h,digits=9)

format(h,nsmall=4)

#results
> h=12345.16711
> h
[1] 12345.17
> round(h,digits=1)
[1] 12345.2
> round(h,digits=2)
[1] 12345.17
> round(h,digits=3)
[1] 12345.17
> round(h,digits=4)
[1] 12345.17
> round(h,digits=5)
[1] 12345.17
> signif(h,digits=9)
[1] 12345.17
> 
> format(h,nsmall=4)
[1] "12345.1671"




More information about the R-help mailing list